I was debugging something and discovered some strangeness in JavaScript:
alert(1=='') ==> false alert(0=='') ==> true alert(-1=='') ==> false
It would make sense that an implied string comparison that 0 should = ‘0’. This is true for all non-zero values, but why not for zero?
According to the Mozilla documentation on Javascript Comparison Operators
What’s actually happening is that the strings are being converted to numbers. For example:
1 == '1'becomes1 == Number('1')becomes1 == 1:trueThen try this one:
1 == '1.'becomes1 == Number('1.')becomes1 == 1:trueIf they were becoming strings, then you’d get'1' == '1.', which would be false.It just so happens that
Number('') == 0, therefore0 == ''is true