Home > Web Front-end > JS Tutorial > How Do I Reliably Detect NaN Values After Using parseFloat() in JavaScript?

How Do I Reliably Detect NaN Values After Using parseFloat() in JavaScript?

Susan Sarandon
Release: 2024-12-14 05:50:12
Original
118 people have browsed it

How Do I Reliably Detect NaN Values After Using parseFloat() in JavaScript?

Determining NaN Values in JavaScript

When attempting to parse a non-numeric string as a number using JavaScript's parseFloat() function, it may not always return NaN as expected.

Problem:

In Firefox's JavaScript console, the following statements fail to return true:

parseFloat('geoff') == NaN;

parseFloat('geoff') == Number.NaN;
Copy after login

Solution:

To accurately check for NaN, use the isNaN() function:

isNaN(parseFloat("geoff"))
Copy after login

This code will return true because parseFloat("geoff") returns NaN.

Note:

The isNaN() function can also be used to test any value for NaN, not just numbers. For more details, refer to this guide: How do you test for NaN in JavaScript?

The above is the detailed content of How Do I Reliably Detect NaN Values After Using parseFloat() in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template