JavaScript NaN

Last Updated on 05 August 2020

The NaN is a global property that represent as “Not-a-Number” value.




The NaN is a property originally from window global object.

NaN vs window.NaN vs Number.NaN

No matter NaN or window.NaN or Number.NaN, they are the same and equivalent. To prove you, we use method to check both of them are the same., NaN) // true, Number.NaN) // true, window.NaN) // true, NaN) // true, Number.NaN) // true, window.NaN) // true, NaN) // true, Number.NaN) // true, window.NaN) // true

Don’t Use Comparison Operator

Don’t use the comparison operator to check whether the value is NaN, it can happen unexpected results.

NaN == NaN;        // false
NaN == Number.NaN; // false
NaN === NaN;        // false
NaN === Number.NaN; // false

isNaN(NaN);         // true
isNaN(Number.NaN);  // true
Number.isNaN(NaN);  // true

Use Number.isNaN()

 To check whether a value is a NaN value, we recommend use the Number.isNaN() function.

Number.isNaN(NaN); // true
Number.isNaN(Number.NaN);  // true
Number.isNaN(window.NaN);  // true

Don’t use isNaN()

The reason not to recommend to using isNaN() is because it can also happened unexpected results. When the input is a string, it can also return true, while the Number.isNaN() will return true when it’s only NaN.

isNaN('Jorcus'); // true
Number.isNaN('Jorcus'); // false

When NaN happened?

The NaN is a non-writable, non-enumerable and non-configurable property. It’s hardly see a program will use NaN. Most of the cases we have seen are dealing with math or numbers.

  • 123 ** NaN – Operand of an argument is NaN
  • Infinity - Infinity – When dealing with Infinity
  • Infinity * 0 – Weird math calculation
  • "string"*5 – Involving with strings
  • math.max("hello"); – Number cannot be resolved
  • Math.sqrt(-5); – Result is not a real number

Browser Support


Further Readings

Leave a Comment