Equality operator operation rules are as follows:
1, if the same type of two values, for comparing ===.
(Ps: [] == [], {} == {} of the same type, for comparing ===, where different addresses, return false)
2, if the two values are different types, they may be equal.
The following type conversion rules then compare:
(1) if a is null, a is undefined, then [equal].
(2) If any value is a string, the other is a numerical value, before comparing the first character string into equivalence value; i.e. call Number () function.
(3) If any value is a Boolean value, before comparing for equality first convert the value, i.e., calling Number () function.
(4) If an object, other numeric or string, the value of the underlying object into another type of comparison. It is converted into an object base type, or using its toString valueOf method. js core built-in classes, will try to toString valueOf before; however Date utilization is toString conversion.
(!! Ps: [] == [] == than it is converted to [] == false -> [] == 0 -> Returns true so '== 0)
(Ps:! {} == {} -> {} == false -> {} == 0 -> [object Object] == 0 is returned false)
supplement:
Objects | return value |
---|---|
Array | Returns an array of object itself. |
Boolean | Boolean value. |
Date | Storage time from the beginning of January 1970 midnight on the 1st count of milliseconds UTC. |
Function | Function itself. |
Number | Numeric value. |
Object | The object itself. This is the default. |
String | String value. |
Math and Error objects do not have valueOf method. |
Reproduced in: https: //juejin.im/post/5d0a06516fb9a07eee5ed222