Loops Flashcards

Learn JavaScript Loops (8 cards)

1
Q

For Loop

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Do…While Loop

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

While Loop

A

used to execute a block of code as long as a specified condition is true.

while (condition) {
// code to be executed
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The Labeled Statement

A

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});
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

For…In Loop

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The break Statement

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The continue Statement

A

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);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

For…Of Loop

A

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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly