[0] == 0 and 0 == [[0]] compares the primitive value with the object and thus type conversion will be performed . In both cases, [0] and [[0]] will eventually be converted to a primitive value of 0 .
This is defined in steps 8 (and 9) of the Abstract Equation Comparison Algorithm :
- If type (x) is either a string or a number, and type (y) is an object,
returns the comparison result x == ToPrimitive (y).
However, [0] === [[0]] compares two objects, and two different objects are never equal to each other:
1st Return true if x and y refer to the same object. Otherwise, return false.
Here is a slightly simpler example demonstrating that free comparison is not transitive:
" " == 0 // true "\n" == 0 // true " " == "\n" // false
The first two comparisons perform type conversion (string to number), the latter is not performed, and the values โโof both strings are different.
source share