Skip to content

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Notifications You must be signed in to change notification settings

lassiecoder/100daysofjs

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 

Repository files navigation

Loops in JS

Loops in JavaScript are used to execute a block of code repeatedly until a specified condition is met.

πŸ₯‘ for Loop

πŸ₯‘ while Loop

πŸ₯‘ do...while Loop

πŸ₯‘ for...in Loop

πŸ₯‘ for...of Loop

πŸ₯‘ Nested Loop

πŸ₯‘ break and continue Statements


for loop

Executes a block of code a specified number of times.

For example:

for (let i = 0; i < 5; i++) {
    console.log(i); // Output: 0, 1, 2, 3, 4
}

while Loop

Repeatedly executes a block of code while a specified condition is true.

For example:

let i = 0;
while (i < 5) {
    console.log(i); // Output: 0, 1, 2, 3, 4
    i++;
}

do...while Loop

Similar to while loop, but the block of code is executed at least once, even if the condition is false.

For example:

let i = 0;
do {
    console.log(i); // Output: 0
    i++;
} while (i < 0);

for...in Loop

Iterates over the properties of an object.

For example:

const person = { name: 'John', age: 30, job: 'Developer' };
for (let key in person) {
    console.log(key, person[key]); // Output: name John, age 30, job Developer
}

for...of Loop

Iterates over the values of an iterable object (arrays, strings, etc.).

For example:

const colors = ['red', 'green', 'blue'];
for (let color of colors) {
    console.log(color); // Output: red, green, blue
}

Nested Loop

Using loops within loops.

For example:

for (let i = 0; i < 3; i++) {
    for (let j = 0; j < 3; j++) {
        console.log(i, j); // Output: 0 0, 0 1, 0 2, 1 0, 1 1, 1 2, 2 0, 2 1, 2 2
    }
}

break and continue Statements

break: Terminates the loop. continue: Skips the current iteration and moves to the next one.

For example:

for (let i = 0; i < 5; i++) {
    if (i === 3) break;
    console.log(i); // Output: 0, 1, 2
}

for (let i = 0; i < 5; i++) {
    if (i === 2) continue;
    console.log(i); // Output: 0, 1, 3, 4
}

About

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published