Name
isNaN() — check for not-a-number
Synopsis
isNaN(x)
Arguments
xThe 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);// => falseisNaN(0/0);// => trueisNaN(parseInt("3"));// => falseisNaN(parseInt("hello"));// => trueisNaN("3");// => falseisNaN("hello");// => trueisNaN(true);// => falseisNaN(undefined);// => true
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access