Rationale for why ECMAScript converts primitive values to numbers in == operator comparisons when one is boolean

Brendan Eich brendan at mozilla.com
Sun Jan 6 14:59:23 PST 2013


Brendan Eich wrote:
> If we fixed this botch, we'd still have:
>
> 0 == "0"
> true == "1"
> false == "0"

Sorry, make that:

0 == "0"
1 == "1"
true != "1"
false != "0"

Also:

false != ""

Per my calling the preference for number over string conversion a botch, 
we would not have true == "1" or false == "0", because that narrows from 
string to number. It's true the narrowing loses no bits, and one can 
widen 0 back to "0" and 1 back to "1", but I meant to illustrate what 
removing all number-over-string bias from the implicit conversion spec 
for == would do.

Would such a change break a lot of code on the web? I'd bet large sums 
it would.

> But we would also have what your example wants:
>
> true == "true"

This part stands.

/be


More information about the es-discuss mailing list