10 biggest JS pitfalls

Axel Rauschmayer axel at rauschma.de
Sun Jan 6 18:10:24 PST 2013


> Note that == does not respect truthiness or falsiness:
> 
>     > 2 == true
>     false
>     > 2 == false
>     false
> 
>     > '2' == true
>     false
>     > '2' == false
>     false
> 
> None of these (above) abstract comparison operations represents "truthy" or "falsy" behaviour.
> 
> 0 == false;
> // true
> 11.9.3.7 converts to 0==0
> 
> 
> "" == false;
> // true
> 11.9.3.7 converts to ""==0
> 11.9.3.5 converts to 0==0
> 
> 
> 1 == true;
> // true
> 11.9.3.7 converts to 1==1
> 
> 
> "" becomes +0 per 9.3.1
> +0 becomes false per 9.2
> true becomes 1 per 9.3 table 12
> 
> 
> The unary logical NOT ! also converts with ToBoolean. 
> 
> 2==true is just the number two compared with the Boolean true, which aren't equal in value or type and have no criteria for conversion.

I am aware (2==true converts to 2==1). But I was surprised when I first found out. The above was slightly off-topic, because I misunderstood what trick Andrea was referring to.


-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130107/d2fb98b8/attachment.html>


More information about the es-discuss mailing list