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

北城余情 提交于 2019-12-12 10:56:19

问题


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/


回答1:


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




回答2:


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.




回答3:


When you use litteral array/object initialisation, even if it is empty, you create a new object and it's reference is returned. So when you compare them, you compare the value of the reference of the objects you created.

Your other examples return true because you're comparing different types of variables, so, the objects/arrays are transtyped to be comparable with them.



来源:https://stackoverflow.com/questions/13856597/why-does-and-others-return-false-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!