Javascript isNaN()

The JavaScript isNaN() function checks if a value is NaN (Not-a-Number) or not.

The syntax of the isNaN() function is:

isNaN(value)

isNaN() Parameters

The isNaN() function takes in:

  • value - The value to be tested.

Return value from isNaN()

  • Returns true if the argument is NaN.
  • Returns false for other arguments.

Example: Using isNaN()

console.log(isNaN(NaN)); // true
console.log(isNaN(undefined)); // true

console.log(isNaN(643511)); // false
console.log(isNaN(null)); // false

// inplicit conversion to number
console.log(isNaN("3888.415")); // false
console.log(isNaN("210AA")); // true as Number("210AA") is NaN

console.log(isNaN("")); // false as Number('') is 0

console.log(isNaN(new Date())); // false
console.log(isNaN(new Date().toString())); // true

Output

true
true
false
false
false
true
false
false
true

Notes:

  • isNaN() is a top-level function and is not associated with any object.
  • If the argument is not of type Number, the value is first coerced to NaN and then checked.

Recommended Reading: Javascript isFinite()