# -0 === +0

I came across an interesting tweet from @getify the other day which mentioned the difference between -0 and +0 in JavaScript. This was news to me and it took a few moments to grasp the concept of both a positive and negative zero.

After a bit more reading, it starts to make sense if you recognize that numbers when stored in a binary format are signed or unsigned. If the number is signed, then it must have a sign of + or – which makes sense for numbers like 1, 2 and 3 – just not zero. Like many other programming languages, JavaScript has both -0 and +0 because it follows the standard for floating point arithmetic (IEEE 754) which enforces this behaviour.

Luckily, JavaScript generally treats both -0 and +0 as the same number, thus:

-0 === +0 // true
0 == -0 // true

But there are a few odd quirks, for instance:

-0 + -0 // is -0
-0 + 0 // is 0

Negative zeros can also be tested for in various different ways. One of which is using the Math.atan2() method because it is one of the only methods to return a different result for -0 or +0.

Math.atan2(-0, -1) // -3.141592653589793
Math.atan2(+0, -1); // 3.141592653589793

The commonly accepted solution, however, is to use a function like this:

function isNegativeZero(number) {
return number === 0 && (1 / number < 0);
}

So there we have it, not a common occurrence maybe, but interesting nonetheless.

Image by dc0de_null