Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Global Functions and Values

The NaN value

Reference for JavaScript NaN: how it arises, why NaN !== NaN, reliable ways to test for it, how it spreads through calculations, and sorting caveats.

NaN ("not a number") is a special value of the Number type that signals a failed numeric operation, such as 0 / 0, Math.sqrt(-1) or Number("abc"). Despite its name, typeof NaN is "number".

NaN is the only value in JavaScript that is not equal to itself, which is why you test for it with Number.isNaN() rather than with ===.

JavaScript
Number("abc");   // NaN

Try it

JS Where NaN comes from

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

No output.

JS Testing for NaN

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

No output.

Syntax

JavaScript
NaN
Number.NaN

Value: the Number value that represents an undefined or unrepresentable result.

Property attributes: NaN on the global object is not writable, not enumerable and not configurable.

Usage notes

  • Never compare with === or ==. Both are always false for NaN, even NaN === NaN.
  • Reliable tests: Number.isNaN(x) (no conversion), Object.is(x, NaN), or x !== x. The global isNaN() converts first and reports true for text like "abc".
  • It spreads. Any arithmetic with NaN gives NaN, so one bad value can silently ruin a total. Validate inputs early.
  • Collections: includes() and Set use SameValueZero and treat NaN as equal to itself; indexOf() and ===-based lookups do not find it.
  • Sorting: a comparator that returns NaN is treated as 0, which makes ordering inconsistent. Fix the data or handle NaN explicitly.
  • JSON turns it into null: JSON.stringify(NaN) is "null".
  • Not an error. JavaScript does not throw for invalid arithmetic; it returns NaN. Dates use it too: an invalid Date has a time value of NaN.
  • Boolean context: NaN is falsy, as are 0, "", null and undefined.
  • BigInt has no NaN; 0n / 0n throws a RangeError.

Browser support

NaN works in every browser and runtime.