Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Number and BigInt

Number.isNaN()

Reference for Number.isNaN(): testing for the NaN value without coercion, why NaN !== NaN, and how it differs from the global isNaN() and Object.is().

Number.isNaN() returns true only if its argument is the number value NaN ("not a number"). It does no conversion, so Number.isNaN("hello") is false: the string is not NaN, it is just not a number.

NaN is the one value that is not equal to itself, so value === NaN never works as a test. This method is the clear, correct check.

JavaScript
Number.isNaN(value)

Try it

JS Why NaN needs a special check

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Number.isNaN() versus isNaN()

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
Number.isNaN(value)
Name Description
value The value to test. Any type is accepted.

Return value: true if value is of type number and is NaN; otherwise false.

Exceptions: none.

Usage notes

  • No coercion. The global isNaN() converts first, so isNaN("abc") and isNaN(undefined) are true, and isNaN("") is false because Number("") is 0. Those answers are confusing; prefer Number.isNaN().
  • How NaN is produced: failed conversions (Number("abc")), invalid maths (0 / 0, Math.sqrt(-1), Infinity - Infinity), and arithmetic on undefined.
  • It spreads. Any arithmetic with NaN gives NaN, so a single bad input can turn a whole total into NaN. Validate at the edges.
  • Comparisons with NaN are always false, even NaN <= NaN.
  • Arrays and sets: [NaN].includes(NaN) is true, but [NaN].indexOf(NaN) is -1. Set and Map treat NaN as equal to itself.
  • Equivalent checks: Object.is(value, NaN) and value !== value work too; Number.isNaN() reads better.
  • Not the same as invalid input in general. To check that a value is a usable number, use Number.isFinite() as well, because Infinity is a number but often unwanted.
  • Invalid dates: new Date("oops").getTime() is NaN; check dates with Number.isNaN(date.getTime()).

Browser support

Supported in all current browsers; it arrived with ES2015.