MakeW3web

JavaScript Loops

Loops in JavaScript are used to repeatedly perform a set of actions or execute a block of code multiple times. Loops are particularly useful when you need to process a list of items, like elements in an array or properties in an object.

There are three main types of loops in JavaScript: `for`, `while`, and `do-while`.

  1. `for` loop:

The `for` loop is used when you know how many times you want to perform an action. It has three parts: an initialization, a condition, and an increment/decrement operation.

for (initialization; condition; increment/decrement) {
  // code to be executed
}

Example:

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

Explanation: The loop starts with `i` equal to 1 (`let i = 1`), then checks if `i` is less than or equal to 5 (`i <= 5`). If the condition is true, the code inside the loop is executed, and `i` is incremented by 1 (`i++`). This process continues until the condition is false.

  1. `while` loop:

The `while` loop is used when you don't know how many times you want to perform an action, but you know that you want to keep doing it as long as a certain condition is true.

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

Example:

// Print numbers 1 to 5
let i = 1;
while (i <= 5) {
  console.log(i);
  i++;
}

Explanation: The loop starts with `i` equal to 1, then checks if `i` is less than or equal to 5 (`i <= 5`). If the condition is true, the code inside the loop is executed, and `i` is incremented by 1 (`i++`). This process continues until the condition is false.

  1. `do-while` loop:

The `do-while` loop is similar to the `while` loop, but it guarantees that the code inside the loop will be executed at least once, even if the condition is false from the beginning.

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

Example:

// Print numbers 1 to 5
let i = 1;
do {
  console.log(i);
  i++;
} while (i <= 5);

Explanation: The loop starts with `i` equal to 1, and the code inside the loop is executed. After that, the condition is checked (`i <= 5`). If the condition is true, the loop continues, and `i` is incremented by 1 (`i++`). This process continues until the condition is false.

  1. Loop control statements:
  • `break`: This statement is used to exit a loop early, stopping it from running further, even if the loop condition hasn't been met yet.
  • `continue`: This statement is used to skip the current iteration of the loop and move on to the next one, without executing the remaining code in the current iteration.
Example:
// Print numbers 1 to 4, but skip 3
for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue; // Skips the current iteration when i is equal to 3
  }
  console.log(i); // 1, 2, 4, 5
}

In this example, when `i` is equal to 3, the `continue` statement is executed, skipping the rest of the code in the current iteration and moving on to the next one.

Loops are an essential concept in programming, allowing you to perform repetitive tasks efficiently. By understanding the different types of loops in JavaScript

Was this page helpful?