By the end of this lesson, you will be able to recognize JavaScript's NaN value, explain why it appears, and test for it reliably.
What it is
NaN means “Not-a-Number”. It is a special numeric value produced when a mathematical operation cannot produce a meaningful number. For example, converting a non-numeric string with Number("abc") returns NaN. Although its name says not-a-number, typeof NaN is "number". Related terms include Infinity, -Infinity, Number.isNaN(), and the global isNaN() function.
Think of NaN as a marker that says: “this expression was supposed to be numeric, but the inputs or operation were invalid.” It is not an error and does not stop execution. It can spread through arithmetic: NaN + 1 is NaN.
Why it matters
- It helps you detect invalid conversions, such as
Number("abc")orparseInt("hello", 10). - It prevents silent bugs when calculations produce unexpected results.
- It is common when parsing user input, reading form fields, or processing JSON data.
- It requires special checks because
NaNis not equal to itself.
Syntax or steps
NaN is a global value, so you can use it directly. To create it, perform an invalid numeric operation:
Number("abc"); // NaN
0 / 0; // NaN
Math.sqrt(-1); // NaN
To test for NaN, prefer Number.isNaN(value). It returns true only when value is exactly NaN. Avoid global isNaN(value) unless you understand coercion, because isNaN("abc") is true even though "abc" is a string, not NaN.
Example
const input = "42";
const parsed = Number(input);
if (Number.isNaN(parsed)) {
console.log("Invalid number");
} else {
console.log("Parsed value:", parsed);
}
const bad = Number("abc");
console.log(bad); // NaN
console.log(typeof bad); // "number"
console.log(bad === bad); // false
console.log(Number.isNaN(bad)); // true
console.log(isNaN(bad)); // true
console.log(isNaN("abc")); // true, because "abc" is coerced to NaN
Part by part: Number("42") converts the string to 42. Number.isNaN(parsed) checks whether the conversion failed. Number("abc") produces NaN. typeof bad is "number" because NaN belongs to the Number type. bad === bad is false, which is a key JavaScript rule. Number.isNaN(bad) is the reliable check. The last line shows why global isNaN can be misleading.
Common mistakes
- Using
value === NaN. This is alwaysfalse. UseNumber.isNaN(value)instead. - Assuming
NaNis not a number. It is a special numeric value, sotypeof NaNis"number". - Using global
isNaN()for validation. It coerces values first, soisNaN("abc")returnstrueeven though the value is a string. - Ignoring
NaNin calculations. OnceNaNenters an expression, it often contaminates the result, such asNaN + 5.
When to use it
Use NaN as a signal that an operation cannot produce a valid number. Use Infinity when a calculation overflows or divides a positive number by zero. Use Number.isNaN() to check for NaN. Use global isNaN() only when you intentionally want coercion-based checking.
| Situation | Result | Check |
|---|---|---|
| Invalid numeric conversion | NaN |
Number.isNaN(value) |
| Positive number divided by zero | Infinity |
value === Infinity |
Non-numeric string passed to isNaN |
true |
Prefer Number.isNaN(value) |
Practice
Guided exercise: Write code that converts a variable named rawValue to a number and logs "Bad input" if the result is NaN, otherwise logs the number.
const rawValue = "12px";
const value = Number(rawValue);
if (Number.isNaN(value)) {
console.log("Bad input");
} else {
console.log(value);
}
Expected output: Bad input, because Number("12px") is NaN.
Challenge: Explain why parseInt("12px", 10) returns 12, but Number("12px") returns NaN. Hint: parseInt reads leading digits, while Number requires the whole string to be numeric.
Quick check
Question: What is the safest way to test whether a value is NaN?
Answer: Use Number.isNaN(value). Do not use value === NaN, because NaN is not equal to itself.
Summary
NaN is JavaScript's special numeric marker for invalid numeric results. It is a number type, but it never equals itself, so you must use Number.isNaN() to detect it reliably. Understanding NaN helps you catch bad conversions and avoid silent calculation bugs.