Why 0 == "" true in JavaScript

Why is 0 == "" true in JavaScript? I found a similar message here, but why does the number 0 look like an empty string? Of course, 0 === "" is incorrect.

+25
javascript
Sep 30 2018-11-11T00:
source share
1 answer
 0 == '' 

The left operand is of type Number.
The correct operand is of type String.

In this case, the right operand is forced to the Number type:

 0 == Number('') 

that leads to

 0 == 0 



From the "strong" 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: http://es5.github.com/#x11.9.3

+45
Sep 30 2018-11-11T00:
source share



All Articles