What Is NaN in JavaScript and Why NaN Does Not Equal NaN
NaN is the number value for a failed numeric operation, and it is the one value not equal to itself. See how it arises, how to test for it and how it spreads.

NaN !== NaN is probably the best known oddity in JavaScript, and it is also the most useful to understand, because it explains how to test for the value and why one bad input can ruin a report. The value is not a quirk of the language. It is the standard way floating point arithmetic signals a result that is not a number, and JavaScript inherited the rule from the IEEE 754 standard its number type is built on. This post covers where NaN comes from, the right and wrong ways to detect it, and how it spreads, all on Node.js 24. The numeric type itself is described in how numbers work in JavaScript, and the larger picture is in the guide to types and coercion.
Start with what the type says. NaN stands for "not a number", and its typeof is "number".
console.log(typeof NaN);
console.log(NaN === NaN);
console.log(NaN == NaN);
console.log(NaN !== NaN);number
false
false
trueBoth comparison operators say a NaN is not equal to itself, and !== says it is unequal to itself. That last form is the old detection trick: a value is NaN exactly when value !== value. It works, and it reads as a mistake, so name it if you use it.
How a NaN is produced
Any numeric operation without a defined numeric answer returns NaN. The common sources are failed conversions and impossible arithmetic.
console.log(0 / 0);
console.log(Math.sqrt(-1));
console.log(parseInt("abc", 10));
console.log(Number("12px"));
console.log(Number(undefined));
console.log(undefined + 1);
console.log(Infinity - Infinity);
console.log(Math.max(1, NaN));NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaNDivision of zero by zero, a square root of a negative number, a string that is not fully numeric, an undefined pushed into arithmetic and the difference of two infinities all give NaN. Note that 1 / 0 is Infinity and not NaN, since that result is defined. Math.max(1, NaN) returning NaN is the one that surprises people: the function does not skip invalid inputs.
In real programs, the cause is almost always a missing value reaching arithmetic. A form field that is empty becomes "", which converts to 0, while a missing property is undefined, which converts to NaN. The post on null and undefined shows the two conversions side by side.
The right and wrong ways to test
Because === cannot find a NaN, JavaScript has dedicated tests. There are two functions with the same name pattern and different behavior, and the difference is a classic source of bugs.
console.log(isNaN("abc"));
console.log(Number.isNaN("abc"));
console.log(isNaN(undefined));
console.log(Number.isNaN(undefined));
console.log(Number.isNaN(NaN));
console.log(Object.is(NaN, NaN));true
false
true
false
true
trueThe global isNaN() converts its argument to a number first. "abc" converts to NaN, so it returns true for a string that is not itself NaN. It answers "would this become NaN as a number", which is rarely the question. Number.isNaN() does no conversion and returns true only for the actual value. Use that one, and Object.is(value, NaN) is an equivalent. A lint rule, no-restricted-globals for isNaN, keeps the old function out of a codebase.
When your real question is "is this a usable number", ask that directly.
const isUsableNumber = (value) =>
typeof value === "number" && Number.isFinite(value);
console.log(isUsableNumber(42));
console.log(isUsableNumber(NaN));
console.log(isUsableNumber(Infinity));
console.log(isUsableNumber("42"));true
false
false
falseNumber.isFinite() rejects NaN, Infinity and -Infinity and, unlike the global version, does not coerce strings. Pairing it with a typeof check is redundant for the same reason, since Number.isFinite("42") is already false, but the explicit form says what you mean.
NaN spreads
Arithmetic with a NaN returns NaN, so one bad value contaminates everything computed from it. A total, an average or a chart axis can go wrong long after the input was parsed.
const prices = [20, 5, Number("n/a"), 12.5];
const total = prices.reduce((sum, price) => sum + price, 0);
console.log(total);
const valid = prices.filter((price) => Number.isFinite(price));
const cleanTotal = valid.reduce((sum, price) => sum + price, 0);
console.log(cleanTotal);NaN
37.5One unparseable price made the whole total NaN, and the output gives no hint which price was the cause. The second total excludes the bad value, which may or may not be the right fix: silently dropping a price from an invoice is its own bug. The better habit is to validate where the number is created, so the program either rejects the input or substitutes a documented default, and NaN never enters the calculation. A NaN shown on a page is the visible end of a bug that happened earlier.
Parsing user input without a NaN leak
The reliable place to stop a NaN is the function that turns text into a number. A parser that returns a number or a clear failure keeps the rest of the code simple.
function parseQuantity(text) {
const trimmed = String(text).trim();
if (trimmed === "") return null;
const value = Number(trimmed);
return Number.isFinite(value) ? value : null;
}
console.log(parseQuantity("12"));
console.log(parseQuantity(" 3.5 "));
console.log(parseQuantity(""));
console.log(parseQuantity("12px"));
console.log(parseQuantity("Infinity"));12
3.5
null
null
nullThree decisions are visible in that function. An empty or blank string returns null before conversion, because Number("") is 0 and would turn a blank field into a quantity of zero. Number() is used rather than parseInt() or parseFloat(), because those accept a numeric prefix and would read "12px" as 12. And Number.isFinite() rejects both NaN and the infinities. Returning null is a deliberate choice here: the caller must handle the failure, where a NaN return would let it slide into arithmetic.
Where NaN and equality disagree
Methods that compare values differ on NaN, and knowing which is which saves debugging.
const values = [1, NaN, 3];
console.log(values.indexOf(NaN));
console.log(values.includes(NaN));
console.log(new Set([NaN, NaN]).size);
console.log(new Map([[NaN, "x"]]).get(NaN));
console.log(JSON.stringify({ n: NaN }));-1
true
1
x
{"n":null}indexOf() uses strict equality and cannot find it, while includes() can, as the includes() post explains. A Set stores one NaN, and a Map can use NaN as a key and retrieve it. JSON has no NaN, so serialization turns it into null, which loses information: a number field that was invalid arrives as a deliberate-looking empty value on the other side.
Why equality fails
The rule comes from the standard that defines floating point numbers. IEEE 754 says every comparison involving a NaN is false, except "not equal", which is true. Hardware implements that, and JavaScript exposes it. A language could have special-cased NaN === NaN, and some did, but the hardware comparison is fast and consistent, and the standard has an honest reason: two different failed calculations should not compare as equal, because they did not produce the same number, they produced no number. The post on Object.is versus === covers the two operators that disagree.
Finding where a NaN began
When a NaN shows up in output, the useful question is which step created it, and the answer is the first operation whose inputs were all valid and whose output was not. Working backward from the result is slow. Working forward is quicker: log each intermediate value with Number.isNaN() guarding the print, or add a temporary assertion at the boundary that throws when it sees one.
A throwing check turns a silent contamination into a stack trace that points at the cause.
function assertNumber(value, label) {
if (Number.isNaN(value)) {
throw new TypeError(`${label} is NaN`);
}
return value;
}
try {
const price = assertNumber(Number("n/a"), "price");
console.log(price * 2);
} catch (error) {
console.log(error.message);
}price is NaNWe leave checks like that in code that handles money or measurements, where a wrong number is worse than a crash. In a UI, the same check can fall back to a placeholder instead of throwing, as long as the fallback is visible to the person debugging.
Where this advice stops
If you are dealing with typed arrays of floats, NaN is a legitimate value, such as a missing sample in a data series, and you may want to keep it and test for it in a loop. Use Number.isNaN() there too, since a typed array element is a number.
If a value arrives from a form or a file, test for emptiness before converting, because Number("") is 0, not NaN, and a blank field then counts as zero. That case produces no NaN and a wrong number, which is harder to notice.
The value is defined in the specification under the Number type, and MDN's page for NaN lists how it arises and the equality behavior.


