How do you check that a number is NaN in JavaScript?

How do you check that a number is NaN in JavaScript?

I’ve only been trying it in Firefox’s JavaScript console, but neither of the following statements return true:
parseFloat(‘geoff’) == NaN;

parseFloat(‘geoff’) == Number.NaN;


Solution 1:

Try this code:


For checking whether any value is NaN, instead of just numbers, see here: How do you test for NaN in Javascript?

Solution 2:

I just came across this technique in the book Effective JavaScript that is pretty simple:

Since NaN is the only JavaScript value that is treated as unequal to itself, you can always test if a value is NaN by checking it for equality to itself:

var a = NaN;
a !== a; // true 

var b = "foo";
b !== b; // false 

var c = undefined; 
c !== c; // false

var d = {};
d !== d; // false

var e = { valueOf: "foo" }; 
e !== e; // false

Didn’t realize this until @allsyed commented, but this is in the ECMA spec:

Solution 3:

Use this code:


See isNaN() docs on MDN.

alert ( isNaN('abcd'));  // alerts true
alert ( isNaN('2.0'));  // alerts false
alert ( isNaN(2.0));  // alerts false

Solution 4:

As far as a value of type Number is to be tested whether it is a NaN or not, the global function isNaN will do the work


For a generic approach which works for all the types in JS, we can use any of the following:

For ECMAScript-5 Users:

if(x !== x) {'x is NaN.');
else {'x is NOT a NaN.');

For people using ECMAScript-6:


And For consistency purpose across ECMAScript 5 & 6 both, we can also use this polyfill for Number.isNan

//Polyfill from MDN
Number.isNaN = Number.isNaN || function(value) {
    return typeof value === "number" && isNaN(value);
// Or
Number.isNaN = Number.isNaN || function(value) {     
    return value !== value;

please check This Answer for more details.

Solution 5:

NaN is a special value that can’t be tested like that. An interesting thing I just wanted to share is this

var nanValue = NaN;
if(nanValue !== nanValue) // Returns true!
    alert('nanValue is NaN');

This returns true only for NaN values and Is a safe way of testing. Should definitely be wrapped in a function or atleast commented, because It doesnt make much sense obviously to test if the same variable is not equal to each other, hehe.

Solution 6:

You should use the global isNaN(value) function call, because:

  • It is supported cross-browser
  • See isNaN for documentation


 isNaN('geoff'); // true
 isNaN('3'); // false

I hope this will help you.