0=='0', 0==[] in js are true, '0'==[] is false

Article directory

question

0=='0' and 0==[] are true in js, why '0'==[] is false

insert image description here

analyze

==is a weakly typed comparison

  1. Type conversion occurs when the two types are different
  2. 0 == “0”: First “0”convert to numbertype and then compare;
  3. 0 == []: If there is an object, get the original value of the object first. If the original value is numbera type, compare it directly with 0it; if the original value is a string type, first convert it to a type and then compare; ( the original value numberhere meets the second case )[]“”
  4. “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.

Guess you like

Origin blog.csdn.net/qq_53810245/article/details/132425879