Name

isNaN() — check for not-a-number

Synopsis

isNaN(x)

Arguments

x

The value to be tested.

Returns

true if x is not a number or if it is the special numeric value NaN. It returns false if x is any other number.

Description

“NaN” is an acronym for “not-a-number”. The global variable NaN holds a special numeric value (also known as NaN) that represents an illegal number (such as the result of zero divided by zero). isNaN() tests whether its argument is not a number. This function returns false if x is, or can be converted to, a number other than NaN. It returns true if x is not and cannot be converted to a number, or if it is equal to NaN.

NaN has the special property that it is not equal to any value including itself. So if you want to test specifically for the NaN value, rather than generically for any non-number, do not write x === NaN: that will always be false. Instead use the expression x !== x: this will evaluate to true only if x is NaN.

A common use of isNaN() is to test the results of parseFloat() and parseInt() to determine if they represent legal numbers.

Example

isNaN(0);                  // => false
isNaN(0/0);                // => true
isNaN(parseInt("3"));      // => false
isNaN(parseInt("hello"));  // => true
isNaN("3");                // => false
isNaN("hello");            // => true
isNaN(true);               // => false
isNaN(undefined);          // => true

Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.