question
0=='0' and 0==[] are true in js, why '0'==[] is false
analyze
==
is a weakly typed comparison
- Type conversion occurs when the two types are different
0 == “0”
: First“0”
convert tonumber
type and then compare;0 == []
: If there is an object, get the original value of the object first. If the original value isnumber
a type, compare it directly with0
it; if the original value is a string type, first convert it to a type and then compare; ( the original valuenumber
here meets the second case )[]
“”
“0” == []
: First get[]
the original value, which is the empty string“”
, and then do a comparison“0”
between the two strings“”
. Strings are compared character by character using standard dictionary-based Unicode values, so the two strings are different.
The former two equations are established because of one type conversion, and if the latter is to be established, two type conversions must occur, which should be illegal. The true equation is ''==[], only one type conversion occurs. In fact, if you take str="0", and then take str[0], you will know that this is actually a value, which obviously does not meet the definition of an empty array.