Javascript - Loops And Iteration


Do While

do {
    console.log(i);
} while(i < 10);

For In

for...in statements iterates over all the enumerable properties of an object.

var fruit = ['apple', 'banana', 'cherry']
for(var i in fruit) {
    console.log(i); // '0', '1', '2'
    console.log(fruit[i]); // 'apple', 'banana', 'cherry'
}

For Loop

for(var i; i < 10; i++) {
    console.log(i);
}

For Of

for...of loops through iterable objects, similar to python for..in loops.

var fruit = ['apple', 'banana', 'cherry']
for(var f of fruit) {
    console.log(f); // 'apple', 'banana', 'cherry'
}

Label

break_both_loops: for(var i = 0; i < 10; i++) {
    for(int j = 0; j < 10; j++) {
        if(i + j >) {
            break break_both_loops;
        }
    }
}

Tmp


While

while(i < 10) {
    console.log(i);
}