Javascript isNaN()

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

Example

let number = NaN;

// check if number is NaN
let result = isNaN(number);
console.log("Is number a NaN?", result);

// Output: Is number a NaN? true

isNaN() Syntax

The syntax of the isNaN() function is:

isNaN(value)

isNaN() Parameters

The isNaN() function takes in:

  • value - The value to be tested.

isNaN() Return Value

  • 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.

Also Read:

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community