Skip to content

Loops

Description

JavaScript Loop Statements

for

for ( counter=0; test; counter++)
Examples
for (i=0; i <=5; i++) {
    // Logic
}

let counter=2;
for (; i <= 5; counter++)
    // Logic
    break;
}

for in

  • iterating over enumerable properties of an object
for (let i in object) {
    // Logic for i
}
Example
for (let car in cars) {
    // Logic for car
    console.log(car[cars]]);
}

for of

  • initializes array, then interates over vals of array element or iterable object
for (const i of array){
    // Logic
}

while

while (test) {
    // Logic
}

do while

do {
    // Logic
} while (test);

Labels

Example
for (let i in object) {
    brightnessLoop: while ( object.brightness >= 100) {

        contrastLoop: while (object.contrast <= 50) {
            optimizeValue(this.object);
            continue everythingLoop;
        }
        i++; // Image value optimized
    }
}

Comments