Whoops. Proof that everyone makes mistakes - typo in my second
example! I meant to write:

console.log(foo != 0); //false

On Apr 11, 10:12 pm, Angus Croll <[email protected]> wrote:
> Douglas Crockford is a JavaScript hero and a great communicator. I learned a
> lot from his writings and his excellent video series
> (http://net.tutsplus.com/tutorials/javascript-ajax/crockford-on-javasc...)
>
> We all make mistakes and I'm sure I make more mistakes than Douglas. But
> then Douglas is in a unique position - a generation of JavaScript students
> have grown up quoting his "Crocklamations", insisting that Douglas knows
> best because, after all the Good Parts is The JavaScript Bible.
>
> But, just like anyone, Douglas can get it wrong. Sometimes very wrong.
>
> On page 121 of The Good Parts, in the section "== and !=" Douglas says:
>
> If you want the type coercion, then use the short form. Instead of:
>
>     (foo != 0)
>
> just say
>
>     (foo)
>
> Bad idea. The first is an equality check, the second is a truthey check. The
> coercion rules for the two cases are entirely different. Equality checks
> will convert each value to a primitive, usually a number (using the internal
> ToPrimitive and ToNumber methods). Truthey checks will convert the value to
> a boolean (using the internal ToBoolean method). To see just how different
> the two rules are see this 
> article:http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-j...
> (shameless plug!)
>
> Sometimes both rules will coincidentally arrive at the same result..
>
> var foo = 1;
> if (foo) {
>   console.log(foo != 0); //true
>
> }
>
> but not here...
>
> var foo = "0";
> if (foo) {
>   console.log(foo != 0); //true
>
> }
>
> ...and not here...
>
> var foo = [0];
> if (foo) {
>   console.log(foo != 0); //false
>
> }
>
> ...or here...
>
> var foo = Object("0");
> if (foo) {
>   console.log(foo != 0); //false
>
> }
>
> ...or here!....
>
> var foo = String("0");
> if (foo) {
>   console.log(foo != 0); //false
>
> }
>
> And this is why its dangerous to put all your faith in one teacher, however
> brilliant. Question everything (including me!) and follow your own path.  

-- 
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]

Reply via email to