- Abstract Equality Comparison using
==aka “double equals”
- Strict Equality Comparison using
===aka “triple equals”
ES6 delivered a third option in the form of the
-0 is because it is a part of the IEEE 754 floating-point specification. Two zeroes are also present in other languages like Ruby as well.
The two zeroes in question are:
- Positive zero
- Negative zero
As one might expect, they are treated as “the same” by both equality comparisons methods above. After all, zero is zero.
-0 == +0 // true
-0 === +0 // true
That is where
Object.is comes in. It treats the two zeroes as unequal.
Object.is(+0, -0) // false
Pre-ES6 it is still possible to tell the two zeroes apart. Looking at the results from other operators offers a hint.
-0 > +0 // false
+0 > -0 // false
-0 + -0 // -0
-0 + +0 // +0
+0 + +0 // +0
+1 / -0 // -Infinity
+1 / +0 // +Infinity
The last two operations in particular are useful. Unlike say Ruby or Python where division by zero yields a
Infinity. Now take a look at the polyfill for
Object.is offered on MDN.
- They can be distinguished pre-ES6 and using
- The difference is only evident when doing signed arithmetic