Loops Flashcards
Learn JavaScript Loops (8 cards)
For Loop
The for loop is one of the most commonly used loops in JavaScript. It allows us to execute a block of code a specified number of times.
for (initialization; condition; increment) {
// code to be executed
}
Do…While Loop
similar to the while loop, but with one key difference: the condition is evaluated after the code block has been executed.
do {
// code to be executed
} while (condition);
While Loop
used to execute a block of code as long as a specified condition is true.
while (condition) {
// code to be executed
}
The Labeled Statement
In JavaScript, you can use labels to identify a loop or a block of code. Labels are often used with the break and continue statements to control the flow of the program.
outerLoop: for (let i = 0; i < 9; i++) {
innerLoop: for (let j = 0; j < 9; j++) {
if (i === 1 && j === 1) {
break outerLoop;
}
console.log(i = ${i}, j = ${j}
);
}
}
For…In Loop
used to iterate over the properties of an object. It allows us to access each property of an object and perform a specific action.
for (variable in object) {
// code to be executed
}
The break Statement
used to exit a loop or switch statement prematurely. When the break statement is encountered, the program flow immediately moves to the next statement outside of the loop or switch.
for (let i = 0; i < 7; i++) {
if (i === 5) {
break;
}
console.log(i);
}
The continue Statement
used to skip the current iteration of a loop and move on to the next iteration.
for (let i = 0; i < 10; i++) {
if (i === 9) {
continue;
}
console.log(i);
}
For…Of Loop
used to iterate over iterable objects, such as arrays, strings, and other iterable built-in objects. It provides a simpler and more concise syntax compared to the for loop or the for…in loop.
for (variable of iterable) {
// code to be executed
}