Number.NaN
Holds the Not-A-Number value, the result of arithmetic that has no meaningful numeric answer.
Number.NaN- Added in JavaScript ES1
Return value
numberThe Not-A-Number value — the same value as the global NaN. The property is non-writable, non-enumerable and non-configurable.
Errors
Nothing on read; assigning to it throws a TypeError in strict mode, because the property is non-writable.
Changelog
- PHP ES2015Number.isNaN() was added. Unlike the global isNaN(), it does not coerce its argument, so Number.isNaN("abc") is false while isNaN("abc") is true.
- PHP ES2016Array.prototype.includes() uses SameValueZero, so [NaN].includes(NaN) is true even though [NaN].indexOf(NaN) is still -1.
NaN is the IEEE 754 "not a number" value, identical to the global NaN. Despite the name its type is "number". It comes from operations with no defined numeric answer — 0 / 0, Math.sqrt(-1), Infinity - Infinity — and, far more often in practice, from a failed conversion: Number("12px"), parseInt("abc"), undefined + 1.
When to reach for it
Return it deliberately from a numeric function whose input could not be interpreted, so the failure stays inside the number type instead of forcing callers to handle null. Its defining property helps here: NaN propagates, so a single bad value in a chain of arithmetic makes the whole chain NaN rather than producing a plausible wrong figure.
What trips people up
NaN is the only value in JavaScript not equal to itself. x === Number.NaN is always false, so a comparison written that way is dead code that no linter reads as such. Use Number.isNaN(x), or Object.is(x, NaN).
Prefer Number.isNaN() over the global isNaN(): the global coerces first, so isNaN("abc") and isNaN(undefined) are both true for values that are not NaN at all. And sorting is not safe — a comparator returning NaN leaves the array in an arbitrary order with no error.
Example
When to use it
- Signalling that a parse or conversion failed without leaving the number type.
- Filtering rows whose numeric field failed to convert: values.filter(Number.isNaN).
- Tracing where a calculation went wrong, since NaN propagates through every later operation.
Frequently asked questions
Why does NaN === NaN return false?
IEEE 754 defines NaN as unordered and unequal to every value including itself, and JavaScript follows that. Use Number.isNaN(x) or Object.is(x, NaN) to test for it; === can never work.
Why is typeof NaN "number"?
Because NaN is a value inside the double-precision number type — a specific bit pattern reserved for results with no numeric answer. It means "this is a number-typed slot holding an invalid result", not "this is not a number".
What is the difference between Number.isNaN() and isNaN()?
Number.isNaN() returns true only for the actual NaN value, while the global isNaN() coerces its argument first, so isNaN("abc") and isNaN(undefined) are true. Prefer Number.isNaN() unless you specifically want the coercion.
More examples
Number.isNaN() versus the global isNaN()
The two functions disagree on everything that is not already a number. The global isNaN() converts its argument first, so it reports true for "abc" and undefined — neither of which is NaN — and false for "", which converts to 0. Number.isNaN() answers the question people actually mean.
console.log(Number.isNaN(NaN), isNaN(NaN)); // true true
console.log(Number.isNaN("abc"), isNaN("abc")); // false true
console.log(Number.isNaN(undefined), isNaN(undefined)); // false true
console.log(Number.isNaN(""), isNaN("")); // false false
// The global answers "would this coerce to NaN?", not "is this NaN?"
console.log(Number("abc")); // NaN
console.log(Number("")); // 0NaN in collections behaves inconsistently
includes() and Set use SameValueZero, which treats NaN as matching itself, so both find it. indexOf() uses ===, which never matches NaN, so it reports -1 on an array that plainly contains one. The sort silently does nothing useful because the comparator returns NaN for every pair involving it.
const list = [1, Number.NaN, 3];
console.log(list.includes(NaN)); // true (SameValueZero)
console.log(list.indexOf(NaN)); // -1 (strict equality)
console.log(new Set([NaN, NaN]).size); // 1 (SameValueZero)
console.log(list.findIndex(Number.isNaN)); // 1
console.log([3, NaN, 1].sort((a, b) => a - b)); // [3, NaN, 1] - unsorted
Discussion