Loops¶
Loops are a mechanism that allows you to execute a certain block of code repeatedly. JavaScript offers the following types of loops:
for
while
do while
for
loops¶
for
loop looks as follows
for (initial expression; conditional expression; modifier expression ) {
// statements for execution
}
This loop consists of three expressions separated by ;
and each expression is optional:
- initial expression is most often used to initialize a variable used as a count for the number of loop executions
- conditional expression specifies the condition that must be met to make the next iteration of the loop
- the modifier expression is executed after the statement is executed inside the loop body, and is typically used to modify a numerator variable
The following examples show the use of the for
loop:
for (let i = 0; i < 3; i++) {
console.log(i);
}
for (;;) {} // infinite loop
while
loop¶
Like the for
loop, the while
loop is used to perform repetitive actions. The for
loop is most often used when the number of repeated operations is known, and thewhile
loop, when the number of repetitions is unknown, and the end of the loop depends on a certain condition.
The while loop syntax is as follows:
while (condition) {
// statement
}
The code snippet will be repeated as long as the condition is met (i.e. it returns the value true
) and tested in parentheses, e.g.:
let numbers = [1, 2, 3, 4];
while (numbers.length > 0) {
console.log(`I just removed ${numbers.shift()} in a while loop`);
}
/* The following will be displayed
I just removed 1 in a while loop
I just removed 2 in a while loop
I just removed 3 in a while loop
I just removed 4 in a while loop
*/
do while
loop¶
The do while
loop is very similar to thewhile
loop. The only difference between these loops is that the code in the body of the do ... while
loop will be executed before the expression is checked. This means that statements inside a do ... while
loop are executed always at least once even if the check is false. The general form of the do ... while
loop looks like this:
do {
// statements
} while (condition);
For example:
let emptyArray = [];
do {
emptyArray.shift();
} while (emptyArray.length > 0);
Breaking the loop¶
Each loop can be interrupted at any time with the break
statement. When this statement is called inside the loop, it terminates immediately. Loop continuation conditions are not checked in this case. This statement can be used in any type of loop (for
, while
, do while
), e.g .:
let numbers = [1, 2, 3, 4];
while (numbers.length > 0) {
if (numbers[0] % 2 === 0) {
break;
}
console.log(`I just removed ${numbers.shift()} in a while loop`);
}
// only the following will be displayed on the screen: I just removed 1 in a while loop
Continuation of the loop¶
Using the continue
statement inside the loop causes the loop to proceed to its next iteration. So, if a continue
statement is inside the loop, the current iteration will be interrupted and the next one will start (or exit the loop in the case of the last iteration), e.g .:
let numbers = [1, 2, 3, 4];
while (numbers.length > 0) {
if (numbers[0] % 2 === 0) {
numbers.shift();
continue;
}
console.log(`I just removed ${numbers.shift()} in a while loop`);
}
/* On the console, the following lines will be displayed:
I just removed 1 in a while loop
I just removed 3 in a while loop
*/