What Is NaN and How Do You Check for It?
Learn why NaN never equals itself in JavaScript and how to detect it correctly with Number.isNaN() instead of isNaN().
Expected Interview Answer
NaN ("Not a Number") is a special numeric value JavaScript returns when a math operation cannot produce a meaningful number, and by spec it is the only value that is never equal to itself, so you must detect it with Number.isNaN() rather than "===".
Operations like 0 / 0, Number("abc"), or parseInt("hello") all return NaN because there is no valid numeric result. NaN has type “number” (typeof NaN === "number"), which surprises people expecting a distinct type, and per IEEE 754 floating-point rules NaN !== NaN is true for any comparison, including NaN === NaN. The legacy global isNaN() first coerces its argument to a number before testing, so isNaN("hello") returns true even though the string itself is not literally NaN, which is a common source of bugs. Number.isNaN() does not coerce, checking only whether the value is exactly the NaN number, so it is the reliable way to test for it; alternatively, Number.isNaN(x) can be replaced by the idiom x !== x, which is true only for NaN.
- Explains why 0/0 and invalid parses do not throw but instead return NaN
- Clarifies why NaN === NaN is false, unlike every other value
- Distinguishes isNaN() (coerces) from Number.isNaN() (strict, no coercion)
- Gives a safe, reliable way to detect NaN in real code
AI Mentor Explanation
NaN is like a scoreboard slot that reads “no result” after a match is abandoned with no play possible — it is still a valid slot on the board, but comparing “no result” to another “no result” never counts as a match on the results sheet. A naive check that just looks for the word “abandoned” anywhere would flag a rain delay note too, which is misleading. The reliable check is a dedicated official ruling that says specifically “this fixture has no result,” nothing looser. That precise, non-coercing check is exactly what Number.isNaN() does compared to the loose global isNaN().
Step-by-Step Explanation
Step 1
Operation fails to produce a number
e.g. 0/0, Number("abc"), or Math.sqrt(-1) — the spec defines these as returning NaN.
Step 2
NaN is typed as “number”
typeof NaN === "number" even though it represents an invalid numeric result.
Step 3
Equality comparisons fail by spec
NaN === NaN is false, and NaN == NaN is also false — the only value not equal to itself.
Step 4
Detect it correctly
Use Number.isNaN(value) (no coercion) rather than the legacy global isNaN(value), which coerces first.
What Interviewer Expects
- Correct statement that NaN !== NaN by IEEE 754 spec
- Ability to name at least two operations that produce NaN
- Clear distinction between isNaN() and Number.isNaN()
- Knowledge of the x !== x idiom as an alternative detection method
Common Mistakes
- Trying to detect NaN with === or == comparisons
- Using the legacy global isNaN() without realizing it coerces its argument first
- Assuming typeof NaN returns something other than “number”
- Forgetting that Array.prototype.includes() can find NaN (unlike indexOf, which cannot)
Best Answer (HR Friendly)
“NaN stands for "Not a Number" and shows up when a math operation cannot return a valid result, like dividing zero by zero. The tricky part is that NaN never equals itself, so you cannot check for it with a normal equals comparison — instead you use Number.isNaN(), which is built specifically to detect it reliably.”
Code Example
const result = 0 / 0
console.log(result === result) // false — NaN is never equal to itself
console.log(isNaN('hello')) // true — coerces 'hello' to NaN first, misleading
console.log(Number.isNaN('hello')) // false — no coercion, 'hello' is not literally NaN
console.log(Number.isNaN(result)) // true — correct, exact NaN check
// Idiom: NaN is the only value where x !== x
function isReallyNaN(x) {
return x !== x
}Follow-up Questions
- Why does isNaN("hello") return true even though “hello” is not NaN?
- What is the result of typeof NaN and why might that surprise developers?
- How does Array.prototype.includes() handle NaN differently from indexOf()?
- Name three operations besides 0/0 that produce NaN.
MCQ Practice
1. What does NaN === NaN evaluate to?
Per IEEE 754 and the JS spec, NaN is never equal to itself, so the comparison is false.
2. What is the key difference between isNaN() and Number.isNaN()?
The global isNaN() coerces before checking, causing false positives like isNaN("hello") === true.
3. What does typeof NaN return?
NaN is a special numeric value, so typeof NaN is “number”.
Flash Cards
What does NaN stand for? — "Not a Number" — the result of an invalid numeric operation.
Is NaN === NaN true or false? — False — NaN is the only value never equal to itself.
Safest way to detect NaN? — Number.isNaN(value), which does not coerce its argument.
What does typeof NaN return? — "number".