Inline. On Tue, Apr 12, 2011 at 6:12 AM, Angus Croll <anguscr...@gmail.com> 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-javascript-the-complete-series/) > > > 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-javascript/(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 > } > > It's depends from what developers wants to check. If the check is a validation for further computation and we need guarantee that foo is Number, it's important type coercion Like this: foo = 1; foo + 1 is 2, right ? if: foo = "0"; foo + 1 is "01", right ? May be not what we want. I think it depends, in some cases can be redundant in other cases can be source of stupid errors. ...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/jsmentors@jsmentors.com/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/jsmentors@googlegroups.com/ > > To unsubscribe from this group, send email to > jsmentors+unsubscr...@googlegroups.com > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com