The JavaScript NaN Property

NaN is a global property representing a Not-a-Number value. It is a special value that indicates that the value is not a number. NaN is usually the result of an operation that doesn't make sense or is arithmetically undefined, such as the the square root of a negative number:

Math.sqrt(-4); // NaN

Several JavaScript methods return NaN if the value specified in the parameter cannot be converted to or parsed as a number. The following examples all return NaN:

Number("foo");
parseInt("bar");
parseFloat("baz");

NaN is not equal to any other value, including itself. Consequently, you cannot determine whether a given value is not a number by comparing it to NaN. Instead, the isNaN() function can be used to check if the value is not a number:

var test_val = NaN;
test_val == NaN; // false
isNaN(test_val); // true

NaN should not be confused with null or undefined. NaN indicates that the value of an expression is undefined, or is out of its domain of definition.

See also

If you see a typo, want to make a suggestion or have anything in particular you'd like to know more about, please drop us an e-mail at hello at diveintojavascript dot com.

Copyright © 2010-2013 Dive Into JavaScript