What are the different types of loops in JavaScript?

Loops in JavaScript

Loops in JavaScript are used to execute a block of code repeatedly until a specific condition is met. There are several loops in JavaScript, including for, while, do-while, for-in, and for-of loops. This article explains loops in JavaScript in detail with code examples. 

1. For loop

The for loop is the most commonly used loop in JavaScript. It has three parts: the initialization, the condition, and the final expression. The initialization is executed only once at the beginning of the loop, the condition is checked before each iteration, and the final expression is executed at the end of each iteration. The for loop is used when you know the number of iterations you want to perform. It is commonly used to iterate over arrays and other iterable objects.

Example:

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

2. While loop

The while loop executes a block of code as long as a specified condition is true. It checks the condition before each iteration and stops when the condition is false. The while loop is used when you don't know the number of iterations you want to perform in advance. It continues to execute the loop as long as the condition is true.

Example:

let i = 0;
while (i < 10) {
  console.log(i);
  i++;
}

Do-while loop

The do-while loop is similar to the while loop, but it executes the block of code at least once before checking the condition. The do-while loop is similar to the while loop, but it guarantees that the loop body will be executed at least once, even if the condition is false.

Example:

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

4. For-in loop

The for-in loop is used to loop through the properties of an object. The for-in loop is used to loop through the properties of an object. It is commonly used for iterating over the keys of an object.

Example:

const obj = { a: 1, b: 2, c: 3 };
for (const prop in obj) {
  console.log(prop, obj[prop]);
}

5. For-of loop

The for-of loop is used to loop through the elements of an iterable object, such as an array or a string. The for-of loop is used to loop through the values of an iterable object, such as an array or a string. It is commonly used to iterate over arrays.

Example:

const arr = [1, 2, 3];
for (const element of arr) {
  console.log(element);
}

Performance comparison of JavaScript loops

The performance of the different types of loops can vary depending on the specific use case and the size of the data being looped over. However, in general, the for loop is the fastest type of loop in JavaScript because it has the fewest operations to perform.

Here is a brief comparison of the performance of the different types of loops based on a simple test case that involves iterating over an array of 1 million elements:

For loop:

const arr = new Array(1000000);
console.time('for-loop');
for (let i = 0; i < arr.length; i++) {
  // Do something with arr[i]
}
console.timeEnd('for-loop');

// Output: for-loop: 2.822ms

While loop:

const arr = new Array(1000000);
let i = 0;
console.time('while-loop');
while (i < arr.length) {
  // Do something with arr[i]
  i++;
}
console.timeEnd('while-loop');

// Output: while-loop: 3.246ms

Do-while loop:

const arr = new Array(1000000);
let i = 0;
console.time('do-while-loop');
do {
  // Do something with arr[i]
  i++;
} while (i < arr.length);
console.timeEnd('do-while-loop');

// Output: do-while-loop: 3.255ms

For-in loop:

const obj = {};
for (let i = 0; i < 1000000; i++) {
  obj[i] = i;
}
console.time('for-in-loop');
for (const prop in obj) {
  // Do something with obj[prop]
}
console.timeEnd('for-in-loop');

// Output: for-in-loop: 6.373ms

For-of loop:

const arr = new Array(1000000);
console.time('for-of-loop');
for (const element of arr) {
  // Do something with element
}
console.timeEnd('for-of-loop');

// Output: for-of-loop: 9.799ms

As you can see from the above example, the for loop is generally the fastest type of loop, followed by the while loop and the do-while loop. The for-in loop is slower because it involves extra operations to iterate over the properties of an object, and the for-of loop is slower because it involves creating an iterator object and accessing it for each iteration.

However, it's important to note that the performance of loops can vary depending on the specific use case, and the differences between the performance of the different types of loops are usually small. It's usually more important to choose the right loop for the specific task at hand rather than to focus too much on performance differences between them.

Summary

Loops are a fundamental concept in programming and are essential for building many types of applications. They allow you to perform repetitive tasks efficiently and with minimal code. In general, use a for a loop when you know the number of iterations in advance, use a while loop when you don't know the number of iterations, use a for-in loop when you need to iterate over the properties of an object and use a for-of loop when you need to iterate over the values of an iterable object like an array.


Similar Articles