10 biggest JS pitfalls

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Jan 7 08:22:51 PST 2013


Axel, here ... that chapter is really a tiny one and every dev should read
that at least once if JS is, by accident, their programming language:
http://webreflection.blogspot.com/2010/10/javascript-coercion-demystified.html

br


On Sun, Jan 6, 2013 at 6:10 PM, Axel Rauschmayer <axel at rauschma.de> wrote:

> 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/0e636e4a/attachment.html>


More information about the es-discuss mailing list