The Problem with NaN
What you'll learn: Why NaN breaks the normal rules of equality and how this unusual behavior can trip you up.
NaN is Not Equal to Itself
Here's one of JavaScript's strangest quirks: NaN is the only value in the language that is not equal to itself. This violates our everyday understanding of equality — normally, something should always equal itself!
console.log(NaN === NaN); // false (!)
console.log(NaN == NaN); // false
// Even this doesn't work:
const result = 0 / 0; // NaN
console.log(result === result); // false
Why Does This Happen?
This behavior comes from the IEEE 754 floating-point standard. The reasoning: if you have two "not a number" results from different invalid operations, you can't say they represent the same invalid number. Each NaN essentially represents an unknown or undefined mathematical result.
The Right Way to Check for NaN
Since equality operators fail, you must use Number.isNaN():
const invalid = "hello" * 5; // NaN
// WRONG ways to check:
console.log(invalid === NaN); // false (doesn't work!)
console.log(invalid == NaN); // false (doesn't work!)
// RIGHT way:
console.log(Number.isNaN(invalid)); // true ✓
This special method exists precisely because the normal equality checks are unreliable with NaN.
Key Takeaway: NaN is not equal to itself, making === NaN useless for detection. Always use Number.isNaN() to check if a value is NaN.