Home >Web Front-end >JS Tutorial >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!