In David Flanagan\'s Javascript guide, there is a statement:
the == operator never attempts to convert its operands to boolean
So he
What happens under the hood is
If
Type(x)
isBoolean
, return the result of the comparisonToNumber(x) == y
.
Number(false) == ""
followed by
If
Type(x)
isNumber
andType(y)
isString
, return the result of the comparisonx == ToNumber(y)
.
Number(false) == Number("") -> 0 == 0
How can x and y be both true if y is string data type (without conversion)?
They are not both true
, but after type coercion their values are equal.
the == operator never attempts to convert its operands to boolean
And that is correct, if you check the comparison algorithm you will find that types are never implicitly casted to Boolean
.
References: