Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does [] === [] (and others) return false in javascript?

The following comparisons all return false in javascript:

[]===[]
[]==[]
{}==={}
{}=={} 
[0]===[0]
[0]==[0]

However the following return true:

[0]=='0'
[0]==0
[]==false //(and all other == that were exampled above)

What is the reason for this? Especially the difference between [0]!=[0] and [0]==0

Fiddle: http://jsfiddle.net/vnBVj/

like image 881
rickyduck Avatar asked Dec 13 '12 09:12

rickyduck


People also ask

Why does comparing two JavaScript objects always return false?

Comparing two objects like this results in false even if they have the same data. It is because those are two different object instances, they are referring to two different objects. There is no direct method in javascript to check whether two objects have the same data or not.

Why === is false in JavaScript?

Because == (and === ) test to see if two objects are the same object and not if they are identical objects.

Why [] is false?

The reason for [] == false even though [] is truthy is: the comparison [] == false compares the value of [] to false . And to get the value of [] , the JavaScript engine first calls [].

Why is 0 == [] in JS?

In JavaScript “0” is equal to false because “0” is of type string but when it tested for equality the automatic type conversion of JavaScript comes into effect and converts the “0” to its numeric value which is 0 and as we know 0 represents false value. So, “0” equals to false.


2 Answers

This is due to the confusing rules, how javascript does type coercion. You can read about this in §11.9.3 of the EcmaScript 5 spec.

Two Objects (which Arrays are too) are never equal, thus all your comparisons in the first block yield false (§11.9.3.1.c.vi)

The second block is more difficult:

First thing to know is, that == uses type coercion to compare the operands.

When a comparison has a Boolean involved, this one is first coerced to a number.

[]==false
[]==0

after that Objects are coerced to their primitive values by calling Object.prototype.toString

"" == 0

Then the string is coereced to to a number ("" becomes 0)

0 == 0

yielding true. By applying the same rules, you can see why your other examples also yield true.

Note that === never causes type coercion, but checks for correct types first and yields false if they are not equal! Only if the types are equal, it compares the actual values. So this method of comparison is far more reliable than ==.

like image 157
Christoph Avatar answered Sep 30 '22 19:09

Christoph


All the example to result in false can easily be explained by the fact, that in case you are comparing objects (and arrays are special objects), JavaScript will compare object references. As you are creating new objects with all those comparisons, all will point to different objects, hence the result will be false.

As for [0]=='0': As soon as one operand is a string, the other one gets converted to string as well. The string conversion of [0] is '0', thus the result is true.

The same goes for one operand being a number or a boolean, which explains the last two comparisons' results.

For more information have a look at the respective MDN page.

Citing the important part:

Equal (==)

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

like image 41
Sirko Avatar answered Sep 30 '22 19:09

Sirko