JavaScript has two different representations for zero: positive zero (+0) and negative zero (-0), which are equal but can produce different results when dealing with infinity.
Abstract
JavaScript implements the IEEE Standard for Floating-Point Arithmetic (IEEE 754), which has signed zeroes. This means that JavaScript has two different representations for zero: positive zero (+0) and negative zero (-0). These two zeroes are equal in JavaScript, as defined by the ECMAScript section 7.2.13. However, there are differences between the two zeroes when dealing with infinity. Dividing a positive number by negative zero (-0) or a negative number by positive zero (+0) will both produce the result -Infinity. Additionally, negative zero (-0) is generated by division by -Infinity.
Opinions
The existence of signed zeroes in JavaScript is a bit perplexing and can be confusing.
Some programming languages, such as those using two's complement, do not have signed zeroes.
The existence of signed zeroes is a recognized flaw of IEEE-754, Sign-and-Magnitude Representation and One's Complement Representation.
Negative zero (-0) is a rare value in JavaScript and is not likely to cause bugs or errors in code.
If needed, negative zero (-0) can be differentiated from positive zero (+0) using the ES6 comparison method Object.is().
Did you know 1 === 1 and +0 === -0 but 1/+0 !== 1/-0 in JavaScript?
Is Negative Zero (-0) a Number in JavaScript?
JavaScript has a feature called signed zeroes, where positive zero (+0) is the same as unsigned zero (0), but negative zero (-0) is a different value, albeit one that behaves similarly.
“Signed zero is zero with an associated sign. In ordinary arithmetic, the number 0 does not have a sign, so that −0, +0 and 0 are identical.
However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero), regarded as equal by the numerical comparison operations but with possible different behaviors in particular operations.
a. Return the result of performing Strict Equality Comparison x === y.
As you can see, there is no difference in the behavior of == and === when both values are of the same type in JavaScript.
Later in this article, I will explore how to differentiate between the two signed zeroes in JavaScript, since the equality operators evaluate them as equal.
There are not a lot of differences between positive zero +0 and negative zero -0 in JavaScript.
Really, the only main difference has to do with dealing with Infinity. Dividing a positive number by negative zero -0 or a negative number by positive zero +0 will both produce the result -Infinity:
Also, negative zero -0 is generated by division by -Infinity :
That happens because negative zero -0 is generated when positive zero +0 is divided by any negative number:
Otherwise, negative zero -0 is pretty rare in JavaScript.
“Actually, this behavior models limit calculation in math. For example function 1/x has a value infinity in 0, however, it is separated if we are approaching 0 from the positive of negative side; in the former, the result is +inf, in the latter, -inf.”
“It is a recognized flaw of IEEE-754, Sign-and-Magnitude Representation and One’s Complement Representation to have two or more representations for zero [0].”
Some other programming languages circumvent the need for two zeroes using something called two’s complement.
Nevertheless, JavaScript has its two different zeroes, +0 and -0.