Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "0 === -0" true in JavaScript?

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 ?

like image 911
hrishikeshp19 Avatar asked Aug 17 '12 00:08

hrishikeshp19


People also ask

Why is 0 as a string true?

"0" is a string, and since it's not empty, it's evaluated to true.

Why === is false in JavaScript?

Because == (and === ) test to see if two objects are the same object and not if they are identical objects. Most test frameworks will include functions such as deepEqual if you want to see if two objects are identical.

Does 0 evaluate to true in JavaScript?

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN .

Does the integer 0 == false?

0 and 1 are type 'number' but in a Boolean expression, 0 casts to false and 1 casts to true . Since a Boolean expression can only ever yield a Boolean, any expression that is not expressly true or false is evaluated in terms of truthy and falsy. Zero is the only number that evaluates to falsy.


3 Answers

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

like image 161
Cole Tobin Avatar answered Oct 20 '22 18:10

Cole Tobin


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.

like image 6
Kendall Frey Avatar answered Oct 20 '22 17:10

Kendall Frey


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.

like image 3
gray state is coming Avatar answered Oct 20 '22 18:10

gray state is coming