The JavaScript isNaN Function

The JavaScript isNaN global function checks if a value is not a number.

Syntax

var is_nan = isNaN(value);

Parameters

value
The value being evaluated

Examples:

isNaN(42); // false
isNaN("foo"); true
isNaN("3.1415"); // false

Description

The JavaScript isNaN function checks whether a value is not a numeric value. If the value is not a number or cannot be converted to a number, the isNaN() function returns true. Otherwise it returns false.

Note: If the value being evaluated is null or an empty string, isNaN returns false because both null and empty string evaluate to 0.

Numeric values consist of an optional sign (+ or -), a numeral (0–9), an optional decimal point and an optional exponential part.

The isNaN() function is useful to test whether a value is a NaN because NaN is not equal to any other value, including itself:

var test_val = parseInt("foo"); // NaN
isNaN(test_val); // true
test_val == NaN; // false

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