How does this work for a loop? for (; i <length; i ++)

How does this work for a loop? It makes no sense to me.

for ( ; i < length; i++ ) { 
+8
javascript loops
source share
2 answers

The loop will simply repeat until i less than length . It simply assumes that i has already been declared elsewhere.

Actually, all parts within the for loop are optional. For example, this is a perfectly valid way to create an infinite loop:

 for(;;) window.alert('Are you sick of alerts yet?');​​​​​​​​​ 
+13
source share

This is a regular for loop that does nothing at the initialization stage.

This is equivalent to writing:

 ; while (i < length) { // ... i++; } 

unless there is continue in the body ... , in which case the for loop would execute i++ before re-evaluating the condition, and the while would not.

+9
source share

All Articles