Why does JavaScript have -0?

JavaScript has two zeroes: -0 and +0. They are somehow both the same and different.

Thomas Barrasso
Dec 3, 2019 · 2 min read
Using strict equality comparison, -0 and +0 are equal

A Tale of Two Zeroes

Since the early versions of JavaScript there have always been two ways of performing equality comparison:

  • Abstract Equality Comparison using == aka “double equals”

ES6 delivered a third option in the form of the Object.is method. It has some slight differences that hint at problems you did not even know you had. For instance, how would you tell JavaScript’s two zeroes apart?

Perhaps a better question is, JavaScript has two zeroes? Why does JavaScript have two zeroes? Why would any language have two zeroes?

The name for this is Signed Zero. It turns out the reason that JavaScript has -0 is because it is a part of the IEEE 754 floating-point specification. Two zeroes are also present in other languages like Ruby as well.

The two zeroes in question are:

  • Positive zero +0

As one might expect, they are treated as “the same” by both equality comparisons methods above. After all, zero is zero.

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

That is where Object.is comes in. It treats the two zeroes as unequal.

Object.is(+0, -0)  // false

Pre-ES6 it is still possible to tell the two zeroes apart. Looking at the results from other operators offers a hint.

-0 > +0  // false
+0 > -0 // false
-0 + -0 // -0
-0 + +0 // +0
+0 + +0 // +0
+1 / -0 // -Infinity
+1 / +0 // +Infinity

The last two operations in particular are useful. Unlike say Ruby or Python where division by zero yields a ZeroDivisionError, JavaScript returns Infinity. Now take a look at the polyfill for Object.is offered on MDN.

Since division by signed zero in JavaScript returns signed infinity, those results can be compared to tell the two zeroes apart.



  • JavaScript (and many languages) have two, signed zeroes

JavaScript in Plain English

Learn the web's most important programming language.

Thomas Barrasso

Written by

Writing to learn, writing to share. Science, Software & Sarcasm — https://barrasso.me

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade