Signed Zero: +0 and -0
What you'll learn: JavaScript has two distinct zero values—positive zero (+0) and negative zero (-0)—that behave identically in most situations but can be distinguished in special cases.
Two Flavors of Zero
In JavaScript's floating-point number system, zero can have a sign attached to it. This means there's a positive zero (+0 or just 0) and a negative zero (-0). This quirk comes from the IEEE 754 floating-point standard that JavaScript follows.
Think of it like this: imagine you're tracking temperature changes. If the temperature drops toward zero from above, you might land on +0. If it rises toward zero from below, you might land on -0. Both are zero, but they "remember" which direction they came from.
They Compare as Equal
Here's the surprising part: when you compare +0 and -0 using normal equality operators, they're considered equal:
+0 === -0 // true
0 === -0 // true
+0 == -0 // true
For almost all practical purposes, they behave identically. You can use them interchangeably in calculations, and most operations treat them the same way.
Why This Matters
While rare, signed zeros can appear in division operations or certain mathematical computations. You usually don't need to worry about them, but knowing they exist helps you understand edge cases in numerical code.
If you ever need to distinguish between +0 and -0, you can use Object.is() (which you learned earlier) or check the sign of 1/x (positive infinity for +0, negative infinity for -0).
Key Takeaway: JavaScript distinguishes between +0 and -0 internally, but they compare as equal with === and behave identically in most operations—it's a technical detail you'll rarely encounter.