Loops are used to repeatedly execute a block of code as long as a specified condition is true. JavaScript supports several types of loops, each suitable for different scenarios.

  1. for Loop The forloop is used when you know in advance how many times you want to execute a statement or a block of statements.

    Syntax:

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

    Example:

      for (let i = 0; i < 5; i++) {
        console.log(i); // Outputs 0, 1, 2, 3, 4
    }
      
  2. while Loop The while loop repeats a block of code as long as a specified condition is true. It is useful when you do not know beforehand how many times you need to execute the loop.

    Syntax:

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

    Example:

      let i = 0;
    while (i < 5) {
        console.log(i); // Outputs 0, 1, 2, 3, 4
        i++;
    }
      
  3. do...while Loop The do...while loop is similar to the while loop, but it guarantees that the code block is executed at least once because the condition is evaluated after the loop body has been executed.

    Syntax:

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

    Example:

      let i = 0;
    do {
        console.log(i); // Outputs 0, 1, 2, 3, 4
        i++;
    } while (i < 5);
      
  4. for...in Loop The for...in loop is used to iterate over the enumerable properties of an object (including inherited properties).

    Syntax:

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

    Example:

      const person = { name: "John", age: 30, city: "New York" };
    
    for (let key in person) {
        console.log(key + ": " + person[key]); // Outputs name: John, age: 30, city: New York
    }
      
  5. for...of Loop The for...of loop is used to iterate over the values of an iterable object (like an array, string, Map, Set, etc.).

    Syntax:

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

    Example:

      const array = [10, 20, 30, 40];
    
    for (let value of array) {
        console.log(value); // Outputs 10, 20, 30, 40
    }
      

break and continue

Both break and continue statements control the flow of loops in JavaScript. They are used to alter the default behavior of loops, allowing for more flexible and efficient code execution.

  1. break Statement The break statement is used to exit from a loop prematurely. When break is encountered, the loop terminates immediately, and control is transferred to the statement following the loop.

    Syntax:

      break;
      

    Example:

      for (let i = 0; i < 10; i++) {
        if (i === 5) {
            break; // Exits the loop when i equals 5
        }
        console.log(i); // Outputs 0, 1, 2, 3, 4
    }
      

    In this example, the loop terminates when i equals 5, so the numbers 5 through 9 are not logged.

  2. continue Statement The continue statement skips the rest of the code inside the current loop iteration and proceeds to the next iteration of the loop. It is useful for skipping over specific parts of the loop under certain conditions.

    Syntax:

      continue;
      

    Example:

      for (let i = 0; i < 10; i++) {
        if (i % 2 === 0) {
            continue; // Skips the rest of the loop for even numbers
        }
        console.log(i); // Outputs 1, 3, 5, 7, 9
    }
      

    In this example, the continue statement skips even numbers, so only the odd numbers are logged.

Summary

  • for Loop: Ideal for when you know how many times to run the loop.
  • while Loop: Useful for running code while a condition remains true.
  • do...while Loop: Ensures the loop code runs at least once before checking the condition.
  • for...in Loop: Best for iterating over object properties.
  • for...of Loop: Best for iterating over iterable objects like arrays and strings.
  • break: Exits the loop entirely, terminating further iterations.
  • continue: Skips the remaining code in the current iteration and proceeds to the next iteration of the loop.

Using break and continue effectively can help you manage loop execution and optimize your JavaScript code for various scenarios.By mastering these loops, you can effectively control the flow of your JavaScript programs and handle repetitive tasks with ease.