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
Susan SarandonOriginal
2024-12-14 05:50:12120browse

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;

Solution:

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

isNaN(parseFloat("geoff"))

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!

Statement:
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