avatarDr. Derek Austin 🥳

Summary

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.

Photo by Dan Freeman on Unsplash

JavaScript actually has two different representations for zero: positive zero, represented by +0 (or just 0), and negative zero, represented by -0.

This is because JavaScript implements the IEEE Standard for Floating-Point Arithmetic (IEEE 754), which has signed zeroes.

Here is how Wikipedia explains signed zeroes:

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.

This occurs in the sign and magnitude and ones’ complement signed number representations for integers, and in most floating-point number representations.

The number 0 is usually encoded as +0, but can be represented by either +0 or −0.”

Photo by Matthew Pla on Unsplash

Are positive and negative zero equal?

Yes, as expected, positive zero and negative zero equal each other in JavaScript, as seen in the following example:

This behavior comes from ECMAScript section 7.2.13, which defines the Strict Equality Comparison Algorithm (===):

The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows: […]

4. If x is +0 and y is −0, return true.

5. If x is −0 and y is +0, return true.

It is the same for the Abstract Equality Comparison Algorithm (==), which is defined in ECMAScript section 7.2.12:

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows: […]

3. If Type(x) is the same as Type(y), then

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.

Photo by Ricardo Gomez Angel on Unsplash

What’s the difference between +0 and -0?

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.

Photo by Jérôme Prax on Unsplash

Why are there signed zeroes at all?

Naturally, the existence of a separate value for zero that is not quite zero is a bit perplexing. Why would the IEEE standard include two zeroes?

User Agoston Horvath explains why on Stack Overflow:

“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.”

But should it be that way? Chris Hemedinger argues that such behavior is confusing in a comment on the 2ality blog:

“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.

Photo by Aaron Burden on Unsplash

What if you need to differentiate +0 from -0?

Using ES6, you can use the helper method Object.is(), which differentiates between +0 and -0:

Prior to ES6, the only way to differentiate between the two signed zeroes was by comparing whether they produced +Infinity or -Infinity:

It is also worth noting that negative zero (-0) is also a falsy value, meaning it evaluates to false in Boolean expressions, just like positive zero.

Photo by Veliko Karachiviev on Unsplash

Conclusion

Since negative zero -0 is such a rare value in JavaScript, its existence is a bit of JavaScript trivia that may show up as an interview question.

Nevertheless, it is good to know about signed zeroes if you want to master all features of the JavaScript programming language.

Thankfully, since -0 === +0, the existence of positive and negative zeroes is probably not anything that will cause bugs or errors in your code!

But, if you need to differentiate negative zero -0 from positive zero +0, it is easy to do so using the ES6 comparison method Object.is().

Photo by Chris Liu-Beers on Unsplash

Further Reading

Photo by Ricardo Gomez Angel on Unsplash

Dr. Derek Austin is the author of Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months, now available on Amazon.

JavaScript
Programming
Software Development
Coding
Mathematics
Recommended from ReadMedium