Implied string comparison, 0==”, but 1==’1′

According to the Mozilla documentation on Javascript Comparison Operators

If the two operands are not of the same type, JavaScript converts the
operands then applies strict
comparison. If either operand is a
number or a boolean, the operands are
converted to numbers; if either
operand is a string, the other one is
converted to a string

What’s actually happening is that the strings are being converted to numbers.
For example:

1 == '1' becomes 1 == Number('1') becomes 1 == 1: true

Then try this one:
1 == '1.' becomes 1 == Number('1.') becomes 1 == 1: true
If they were becoming strings, then you’d get '1' == '1.', which would be false.

It just so happens that Number('') == 0, therefore 0 == '' is true

Leave a Comment