alert (0 == ''); // true
alert (0 == '0'); // true
I understand that == in javascript performs a conversion and then checks for equality, but how does it perform the conversion in the statements above? Does it convert 0 to ” or ” to 0? Or something else perhaps? Is there a spec somewhere that explains the implementation?
It uses the Abstract Equality Comparison Algorithm.
Specifically for your example
So then you’ll end up with:
…in both cases because an empty string converts to the number
0, and a numeric string converts to the number given.From 9.3.1 ToNumber Applied to the String Type:
Since we’re now doing a comparison of the same Type on the second pass, it will do the following:
To test a
toNumberconversion, you can use the unary+operator.