Why is
0 == "" in JavaScript
>
Since "A" + 0 != "A" + ""
Well, "A"+0 is "A0".
As for 0 == "", that's all done to automatic type conversions and the
fact that javascript considers false, 0, "", undefined all as false, and
the rest as true. So 0 is false and "" is false, therefore 0==""
i think i see your answer to the OP is correct,
but how do you explain the apparent contradiction
0 == ""
AND
0 == "0"
That's not a contradiction, that's just type conversion.
See section 11.8.5 of the emacascript 262 specs:
The Abstract Equality Comparison Algorithm
The comparison x == y, where x and y are values, produces true or
false. Such a comparison is performed as follows:
1. If Type(x) is different from Type(y), go to step 14.
[ snip ]
14. If x is null and y is undefined, return true.
15. If x is undefined and y is null, return true.
16. If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y).
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^
[ snip ]
ToNumber("0") and ToNumber("") both evaluate to 0.
Comment