In JavaScript, loops are used to repeat a block of code multiple times. Here are the different types of loops:

1. For Loop

The for loop runs a block of code a set number of times.

for (let i = 0; i < 5; i++) {
  console.log(i);  // Output: 0, 1, 2, 3, 4
}

Explanation:


2. While Loop

The while loop runs as long as a condition is true.

let i = 0;
while (i < 5) {
  console.log(i);  // Output: 0, 1, 2, 3, 4
  i++;  // Increment `i` inside the loop
}

Explanation:


3. Do-While Loop

The do-while loop is similar to the while loop, but it guarantees that the block of code will run at least once, even if the condition is false initially.

let i = 0;
do {
  console.log(i);  // Output: 0, 1, 2, 3, 4
  i++;
} while (i < 5);

Explanation: