Is there a difference between 0 and -0 in Javascript

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

Tags:

Javascript