DEV Community

loading...
Cover image for Negative zero in JavaScript

Negative zero in JavaScript

rahxuls profile image Rahul ・1 min read

Seriously? This topic! Yes. JavaScript actually has two different representations for ZERO:

  • Positive zero, represented by +0 / 0
  • Negative zero, represented by -0

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

And on a note, both zeroes are equal to one another.

+0 === -0 // true
+0 == -0 // true
Enter fullscreen mode Exit fullscreen mode

The only difference between both is in dealing with Infinity

1 / +0 // Infinity
1 / -0 // -Infinity
-1 / +0 // -Infinity
-1 / -0 // Infinity
Enter fullscreen mode Exit fullscreen mode

Numbers always need to be encoded to be stored digitally. But why do some encodings have two Zeros?

Let us look at encoding an integer as a 4-digit binary number by the sign-and-magnitude method.

Here,

  • One bit denotes the sign. (0 if positive, 1 if negative)
  • Remaining bits for the magnitude. (absolute value)

Therefore, -2 and +2 are encoded as,

1 | 0 | 1 | 0 | => -2

0 | 0 | 1 | 0 | => 2

Which means, we will also have two zeroes!

1000 // -0 
000 // +0
Enter fullscreen mode Exit fullscreen mode

Hunt 🤔

We saw that -0 and 0 are equal.
Imagine, you came across a use case to return false while comparing -0 and 0.

How would you do that??? (Comment below)


😎Thanks for Reading | Happy Coding⚡

Discussion (2)

pic
Editor guide
Collapse
patarapolw profile image
Pacharapol Withayasakpunt • Edited

But, is it the only language that has negative zero?

I thought anything with float will have negative zero, anyway.

Javascript just cheated, because you normally won't have floating point equality.

Collapse
baenencalin profile image
Calin Baenen

Thanks for this. This was informational(*?) to me.