Hacker News new | past | comments | ask | show | jobs | submit login

An introduction really shouldn't encourage things like "for (var i = 0, item; item = a[i]; i++)". Someone's going to cut-and-paste that because it looks cute, and their code will break when their array suddenly needs to contain null values. Fuzzy boolean semantics are bad enough without spreading stuff like this around (speaking from experience with || abuse in Perl).



To be fair, they do note this shortcoming.

"Note that this trick should only be used for arrays which you know do not contain "falsy" values (arrays of objects or DOM nodes for example). If you are iterating over numeric data that might include a 0 or string data that might include the empty string you should use the i, j idiom instead."


So it should be used approximately never in real code.


I also think it should discourage modifying built-in objects. Unlike their for loop, the following line comes with no warning or disclaimer: "Interestingly, you can also add things to the prototype of built-in JavaScript objects."


Agreed. But what is the point of declaring the item variable? Wouldn't "for (var i = 0; a[i]; i++)" mean the same thing, but more concise?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: