isNaN()

isNaN(value)
Returns: boolean · Added in ves1 · Updated March 14, 2026 · Global Functions
global nan isnan coercion

The global isNaN() function determines whether a value is NaN (Not-a-Number). Unlike Number.isNaN(), the global version coerces its argument to a number before testing.

Syntax

isNaN(value)

Parameters

ParameterTypeDescription
valueanyThe value to check

Return Value

Returns true if the value is NaN after coercion to a number, false otherwise.

Examples

Basic Usage

isNaN(NaN);           // true
isNaN(0 / 0);         // true (produces NaN)
isNaN("hello");       // true (coerced to NaN)

Values That Return False

isNaN(42);            // false (42 is a number)
isNaN("42");          // false ("42" coerces to 42)
isNaN(null);          // false (null coerces to 0)
isNaN(undefined);     // true (undefined coerces to NaN)
isNaN({});            // true (object coerces to NaN)

The Coercion Problem

The global isNaN() coerces its argument first, which leads to surprising behavior:

isNaN("hello");       // true  — "hello" becomes NaN
isNaN("42");          // false — "42" becomes 42
isNaN(undefined);    // true  — undefined becomes NaN
isNaN(null);          // false — null becomes 0
isNaN(true);          // false — true becomes 1
isNaN(false);         // false — false becomes 0

Safer Alternative: Number.isNaN()

If you need to check for NaN without coercion, use Number.isNaN():

Number.isNaN(NaN);           // true
Number.isNaN("hello");      // false (string is not NaN)
Number.isNaN(undefined);    // false
Number.isNaN({});           // false (object is not NaN)

Practical Example: Input Validation

When validating user input that should be a number:

function processNumber(input) {
  if (isNaN(input)) {
    return "Please enter a valid number";
  }
  return input * 2;
}

processNumber("hello");   // "Please enter a valid number"
processNumber("42");     // 84 (coerced)
processNumber(21);       // 42

For stricter checking where strings are rejected outright:

function strictNumberCheck(input) {
  if (typeof input !== "number" || Number.isNaN(input)) {
    return "Not a valid number type";
  }
  return input * 2;
}

strictNumberCheck("42");     // "Not a valid number type"
strictNumberCheck(21);       // 42
strictNumberCheck(NaN);     // "Not a valid number type"

See Also