Reputation: 14081
Why is 0 == ""
true in JavaScript? I have found a similar post here, but why is a number 0 similar an empty string? Of course, 0 === ""
is false.
Upvotes: 40
Views: 20668
Reputation: 185893
0 == ''
The left operand is of the type Number.
The right operand is of the type String.
In this case, the right operand is coerced to the type Number:
0 == Number('')
which results in
0 == 0
From the Abstract Equality Comparison Algorithm (number 4):
If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
Source: https://es5.github.io/#x11.9.3
Upvotes: 77