How does this for loop work? It doesn't make sense to me.
for ( ; i < length; i++ ) {
The loop will simply repeat as long as i is less than length. It simply assumes i is already declared elsewhere.
Actually, all parts within a for loop construct are optional. For example, this is a perfectly valid way to create an endless loop:
for(;;) window.alert('Are you sick of alerts yet?');
It's a regular for loop that does nothing at all in the initialization step.
This is equivalent to writing:
;
while (i < length) {
// ...
i++;
}
except if there's a continue in the ... body, in which case the for loop would execute the i++ before re-evaluating the condition, and the while loop would not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With