12 ms·
Unless I'm mistaken, wouldn't this loop terminate prematurely if the array contains any falsy value, such as undefined, null, false, NaN, or 0?
by Drakim 13y ago
Unless I'm mistaken, wouldn't this loop terminate prematurely if the array contains any falsy value, such as undefined, null, false, NaN, or 0?
- joebadmo 13y agoYes, there's a note right after: 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, len idiom instead.
- Wintamute 13y agoYou're correct. Seems like a pretty useless/dangerous idiom to me. How often can you be sure that in 100% of cases your loop will never iterate a 0, an empty string or some other falsey value?