Skip to content

NaN — Not a Number

NaN is a special numeric value representing the result of an invalid or undefined mathematical operation. Despite its name, typeof NaN is 'number'.

NaN appears when calculations fail. Knowing how to detect and handle NaN prevents silent bugs from propagating through your code.

NaN is like a corrupted measurement. The reading says “Not a Measurement” but it’s still in the measurement column. If you don’t check for it, your averages will be wrong.

flowchart TD
Operation["0 / 0"]
Parse["parseInt('abc')"]
Math["Math.sqrt(-1)"]
Coercion["Number(undefined)"]
Operation --> NaN
Parse --> NaN
Math --> NaN
Coercion --> NaN
NaN --> Problem["NaN propagates through calculations"]
Problem --> Result["Any operation with NaN returns NaN"]
0 / 0; // NaN
parseInt('abc'); // NaN
Math.sqrt(-1); // NaN
Number(undefined); // NaN
NaN + 5; // NaN — propagates!

NaN is the only value in JavaScript that is not equal to itself:

NaN === NaN; // false — unique!
NaN == NaN; // false
// ✅ Correct
Number.isNaN(NaN); // true
Number.isNaN('abc'); // false — doesn't coerce
Number.isNaN(NaN + 5); // true
// ❌ Wrong — coerces first
isNaN('abc'); // true — because Number('abc') is NaN
isNaN('123'); // false
// ✅ Alternative
function isValueNaN(value) {
return typeof value === 'number' && isNaN(value);
}
  • Always use Number.isNaN() instead of global isNaN()
  • Validate numeric inputs before performing operations
  • Use fallback values for failed calculations
  • NaN is a number type representing invalid math
  • NaN !== NaN — it’s not equal to itself
  • Use Number.isNaN() for reliable checking
  • NaN propagates through all arithmetic operations