View on GitHub

Matt's homepage

Bookmark this to keep an eye on new things I am learning.

Loops

Return Home

For

Loop a preset number of times

// loop 20 times, starting at 1 until we get to 20
for (let index = 1; index <= 20; index++) {
    console.log(index);
}
console.log('For done!');

^ back to top ^

While

Condition set upfront. Good for loops that depend on external factors. Will run as long as the condition is true

let cntr = 0;
while (cntr != 5) {
    console.log('Not done yet');
    cntr++;
}
console.log('While done!');

^ back to top ^

Do-while

Runs the code at least once before checking the condition. Condition checked after each itterration

let i = 0;
do {
    console.log(i);
    i++;
} while (i < 5)
console.log('do-while done!');

^ back to top ^

For-in

Itterates over keys - Great for when you want to itterate over object keys

const matt = {
    favColour: "purple nurple",
    spiritAnimal: "brush turkey",
    hobby: "Hommus"
    };
for (const k in matt) {
    console.log(k + ':', matt[k]);
}
console.log('for-in done');

^ back to top ^