I\'ve been playing around with arrays in JavaScript and cannot figure out why this happens:
Since the complete answer is never given and I actually understand it now, I'll provide the answer myself.
I found this in the Ecma-262 pdf:
It basically reads that [] == 0 is the same as Number([]) == 0 which is the same as 0 == 0 which is true. This does not apply to strict ===.
There is no rule to compare objects other then rule number one, which is x is the same as y. This means the same in everything, also memory address. Since they are not sharing the same memory address, rule 10 applies (return false).
The comparison
x == y, wherexandyare values, produces true or false. Such a comparison is performed as follows:
If
Type(x)is the same asType(y), thena. Return the result of performing Strict Equality Comparison
x === y.If
xisnullandyisundefined, return true.- If
xis undefinedandyisnull, return true.- If
Type(x)isNumberandType(y)isString, return the result of the comparisonx == ToNumber(y).- If
Type(x)isStringandType(y)isNumber, return the result of the comparisonToNumber(x) == y.- If
Type(x)isBoolean, return the result of the comparisonToNumber(x) == y.- If
Type(y)isBoolean, return the result of the comparisonx == ToNumber(y).- If
Type(x)is eitherString,Number, orSymbolandType(y)isObject, return the result of the comparisonx == ToPrimitive(y).- If
Type(x)isObjectandType(y)is eitherString,Number, orSymbol, return the result of the comparisonToPrimitive(x) == y.- Return false