On Monday, 18 April 2011 18:22:53 UTC-3, Angus Croll wrote: > > Hey Jao > > I'm not advocating for or against coercion I'm just pointing out Douglas > made a mistake on that page > > He strongly implied that if(foo) and if(foo != 0) are the same. But they > are not the same and he completely avoided a discussion of why they are not > the same. > > His words: > f you want the type coercion, then use the short form. Instead of: > (foo != 0) > just say > (foo) > > Well, the discussion about type coertion seems to be in the Appendix B - Bad Parts:
"[... ] JavaScript has two sets of equality operators: === and !==, and their *evil twins* [sic] == and !=. The *good ones* [sic] work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. *The rules by which they do that are complicated and unmemorable*. These are some of the interesting cases: '' == '0' // false 0 == '' // true 0 == '0' // true false == 'false' // false false == '0' // true false == undefined // false false == null // false null == undefined // true ' \t\r\n ' == 0 // true The lack of transitivity is alarming. My advice [Crockford's, not mine] is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.'' Cheers! JR -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
