Take the 2-minute tour ×

In a recent post on http://wtfjs.com/. An author writes following without explanation which happens to be true.

0 === -0 //returns true

My understanding about === operator is it returns true if operands point to same object.

Also, - operator returns a reference to negative value of operand. With this rule, 0 and -0 should not be the same.

So, why is 0 === -0 ?

share|improve this question

3 Answers 3

up vote 6 down vote accepted

In fact, 0 and -0 are not the same even at the bit level. However, there is a special case implemented for +/-0 so they compare as equal.

The === operator compares by value when applied to primitive numbers.

share|improve this answer
It ones complement they aren't the same, but in twos they are. –  Cole Johnson Aug 17 '12 at 0:41
@ColeJohnson I forgot about two's complement for a bit. Javascript uses IEEE 64-bit floating point for numbers, which is one's complement. –  Kendall Frey Aug 17 '12 at 0:43
so it seems special case handled in JavaScript. –  riship89 Aug 17 '12 at 0:47
That's right. I don't think the special case is specific to Javascript either. –  Kendall Frey Aug 17 '12 at 0:48

=== does not always mean point to the same object. It does on objects, but on value types, it compares the value. Hence how this works:

var x = 0;
var y = 0;
var isTrue = (x === y);
document.write(isTrue); // true

JavaScript used IEEE floating point standard where 0 and -0 are two different numbers, however, the ECMAScript standard states the parser must interpret 0 and -0 as the same:

§5.2 (page 12)

Mathematical operations such as addition, subtraction, negation, multiplication, division, and the mathematical functions defined later in this clause should always be understood as computing exact mathematical results on mathematical real numbers, which do not include infinities and do not include a negative zero that is distinguished from positive zero. Algorithms in this standard that model floating-point arithmetic include explicit steps, where necessary, to handle infinities and signed zero and to perform rounding. If a mathematical operation or function is applied to a floating-point number, it should be understood as being applied to the exact mathematical value represented by that floating-point number; such a floating-point number must be finite, and if it is +0 or -0 then the corresponding mathematical value is simply 0.

share|improve this answer
so, is it a special case implemented in JavaScript or negative numbers are evaluated using 2's complement? –  riship89 Aug 17 '12 at 0:45
@hrishikeshp19 IEEE floating point standard is ones complement with the exception of this. –  Cole Johnson Aug 17 '12 at 0:46
It's not just the parser. a === -a where a is 0 returns true. –  Kendall Frey Aug 17 '12 at 0:47
@ColeJohnson Nice excerpt (by itself). I didn't realize it was so blunt about the handling of -0. –  user166390 Aug 17 '12 at 0:55
@pst Neither did I until I tried searching the standard PDF for negative to find the block –  Cole Johnson Aug 17 '12 at 0:58

Primitive numbers are not objects. You're doing a value comparison of the numbers, not an identity comparison of objects.

positive zero is equal to negative zero.

This is from the comparison algorithm for the === operator

If Type(x) is Number, then

  • If x is NaN, return false.

  • If y is NaN, return false.

  • If x is the same Number value as y, return true.

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

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

  • Return false.

share|improve this answer
+1. Because -0 is actually a number in JavaScript (due to all numbers really being IEEE-754 floats, which can represent negative zero). It is just defined to be equal to 0 so people don't go crazy. –  user166390 Aug 17 '12 at 0:49

Your Answer


