12 - JavaScript - Looping Statements

12 - JavaScript - Looping Statements

Looping statements in JavaScript allow you to execute a block of code repeatedly. There are five types of loops in JavaScript: for, for/in, for/of, while, and do/while.

Each of these loops is used for a specific purpose, and the choice of which one to use depends on the specific requirements of your program.

for loop

The for loop is used to iterate over a block of code a fixed number of times. It consists of three parts - initialization, condition, and update statement.

Syntax:

for (initialization; condition; update statement) {
  // code to be executed
}

Here is an example:

for (let i = 0; i < 5; i++) {
  console.log(i);
}

In this example, the loop will execute five times. The let i = 0 initializes the value of the loop variable i to 0, the i < 5 is the condition that checks if i is less than 5, and i++ is the update statement that increments the value of i by 1 in each iteration. The console.log(i) statement will print the value of i in each iteration of the loop.

Output:

0
1
2
3
4

for/of loop

The for/of loop is used to iterate over the values of an iterable object like arrays, strings, and sets.

Syntax:

for (variable of iterable) {
  // code to be executed
}

Here is an example:

const arr = [1, 2, 3];

for (let val of arr) {
  console.log(val);
}

In this example, the loop will iterate over the values of the arr array, which are 1, 2, and 3. The loop assigns each value to the val variable. The console.log(val) statement will print the value in each iteration of the loop.

Output:

1
2
3

for/in loop

The for/in loop is used to iterate over the properties of an object. It works by assigning each property of the object to a variable and executing the code block for each property.

Syntax:

for (variable in object) {
  // code to be executed
}

Here is an example:

const obj = {a: 1, b: 2, c: 3};

for (let prop in obj) {
  console.log(prop, obj[prop]);
}

In this example, the loop will iterate over the properties of the obj object, which are a, b, and c. The loop assigns each property to the prop variable and then uses obj[prop] to access the value of the property. The console.log(prop, obj[prop]) statement will print the property name and its value in each iteration of the loop.

Output:

a, 1
b, 2
c, 3

while loop

The while loop is used to execute a block of code while a specified condition is true. It is commonly used when the number of iterations required is not known before the loop starts.

Syntax:

while (condition) {
  // code to be executed
}
let i = 0;

while (i < 5) {
  console.log(i);
  i++;
}

In this example, the loop will execute as long as i is less than 5. The console.log(i) statement will print the value of i in each iteration of the loop, and i++ increments the value of i by 1 in each iteration.

Output:

0
1
2
3
4

do/while loop

The do/while loop is similar to the while loop, but it executes the block of code at least once, even if the condition is false.

Syntax:

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

Here is an example:

let i = 0;

do {
  console.log(i);
  i++;
} while (i < 5);

In this example, the loop will execute at least once, and then continue to execute as long as i is less than 5. The console.log(i) statement will print the value of i in each iteration of the loop, and i++ increments the value of i by 1 in each iteration.

Output:

0
1
2
3
4

Summarizing Up

Looping statements in JavaScript are used to execute a block of code repeatedly. The five types of loops in JavaScript are: for, for/in, for/of, while, and do/while. The for loop is used to execute a block of code a fixed number of times, for/in is used to iterate over the properties of an object, for/of is used to iterate over the values of an iterable object, while is used to execute a block of code while a specified condition is true, and do/while is similar to the while loop but executes the block of code at least once. The choice of loop depends on the specific requirements of the program.

Did you find this article valuable?

Support Bosonique ITEdTech by becoming a sponsor. Any amount is appreciated!