What do the two expressions var i = 0, item; item = a[i++]; mean?
for (var i = 0, item; item = a[i++];) {
// Do something with item
}
Apparently this is an alternative to
for (var i = 0; i < a.length; i++) {
// Do something with a[i]
}
Is telling the loop to keep going so long as
itemgets assigned a “truthy” value. After each iteration,itemis assigned the next item in the array. The idea is that onceigets to a point where it’s beyond the bounds of the array,undefinedwill be assigned, and the loop will terminate.But whoever wrote this code should be fired since the loop will also terminate if the array contains any “falsy” values: 0, empty string, falseOk, this code was written by the Mozilla folks, and they’re much smarter than I am. Just note that the loop will terminate if the array contains any “falsy” values:0,empty string,falseTo see for yourself:
note that the loop terminates after 3, since 0 is falsy.