In Javascript when you multiply
1 * 0 = 0
whereas
-1 * 0 = -0
Well, I can understand its technically 0, is there something different between 0 and -0 under the hood?
Interesting! It seems their values are equal--neither is larger than the other, but they are distinct objects with several side effects (including division by 0
or -0
as per Roisin's answer).
Other interesting quirks observed:
const a = 0;
const b = -0;
a == b; // true
a === b; // true
a < b; // false
b < a; // false
Object.is(a, b); // false
Object.is(a, -b); // true
b.toString(); // "0" <-- loses the negative sign
a + b; // 0
b - a; // -0
a * b; // -0
Yes, there is a difference. JavaScript has signed zeros, so the two are represented differently internally.
There are some practical differences too:
console.log(1 / +0 === Infinity) // true
console.log(1 / -0 === -Infinity) // true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With