In JavaScript, there are 2 different zeros. A regular 0
is a bit different from -0
in JS.
If you divide a zero by any negative number, you’ll get -0
.
The only reason why minus zero exists in JS is the fact that JavaScript implements the IEEE Standard for Floating-Point Arithmetic (IEEE 754), which has signed zeroes.
Let’s take a look at how 0
and -0
look like when logged to the console and how these values compare to each other.
const zero = 0;
const minusZero = zero / -1;
console.log(zero); // 0
console.log(minusZero); // -0
console.log(zero === minusZero); // true
As you see, even though 0
and -0
appear differently when logged to the console, they appear
equal when compared using the strict equality operator.
If you need a function that would tell the explicit difference between 0
and -0
in JavaScript,
you can use Object.is
.
Object.is(-0, +0); // false