Special Cases: NaN, -0, and +0
You'll learn why JavaScript's equality rules have surprising exceptions for three special values.
The Core Idea
Even with ===, JavaScript has quirks. The most notorious: NaN is not equal to itself. Additionally, JavaScript distinguishes between -0 and +0 internally, but treats them as equal with ===.
NaN: The Oddball
NaN (Not-a-Number) is the only value in JavaScript that doesn't equal itself:
NaN === NaN // false — surprising!
Why? The IEEE 754 floating-point standard (which JavaScript follows) dictates that any operation producing an indeterminate result should never equal another indeterminate result. Think of it like asking "Is unknown equal to unknown?" — technically, you can't know.
To check if something is NaN, use typeof logic or built-in checks (we'll cover methods later), but never compare it to itself.
-0 and +0: Identical Twins
JavaScript has both negative zero and positive zero:
-0 === +0 // true
-0 === 0 // true
They're considered equal by ===, even though they're technically distinct values. Most of the time, this doesn't matter. You'll rarely encounter -0 unless doing specific mathematical operations (like Math.round(-0.4)).
Why does -0 exist? The floating-point standard includes it to preserve sign information in calculations near zero, useful in advanced scientific computing.
The Analogy
Think of NaN as a question mark — "Is ? equal to ?" makes no sense. Think of -0 and +0 as identical twins wearing different name tags — JavaScript sees them as the same person.
Key Takeaway: NaN never equals itself (even with ===), and -0 equals +0 despite being different values — these are special cases baked into JavaScript's number system.