When abstractly comparing a string and a number, regardless of the order, the string will be converted ToNumber()
for the comparison:
4. If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y).
5. If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y.
In the case of 0 == ""
, ToNumber("")
results in 0
, which is exactly the other value:
0 == "" // becomes...
0 == 0 // becomes...
true
Note: You can see how the internal-onlyToNumber()
handles different values by using the Number()
constructor without new
:
console.log(Number(""))
// 0