Break & Continue in Loop


In JavaScript, break and continue are two powerful statements that allow for precise control over the execution flow within loops. The break statement terminates the loop entirely, while the continue statement skips the current iteration and proceeds to the next. Understanding how to use these statements effectively is a core skill for any JavaScript developer, enabling more efficient and readable code.


Example 1: Basic break in a for loop

// This loop will terminate when the counter reaches 5
for (let i = 0; i < 10; i++) {
  if (i === 5) {
    break; // Exit the loop when i is 5
  }
  console.log(i); // This will log numbers 0 through 4
}

Explanation

This for loop is designed to run ten times. However, the break statement is triggered when the variable i equals 5, causing the loop to immediately terminate.


Example 2: break in a while loop

// This loop will end when a condition is met
let i = 0;
while (i < 10) {
  if (i === 7) {
    break; // Exit the loop when i is 7
  }
  console.log(i); // Logs numbers 0 through 6
  i++;
}

Explanation

The while loop continues as long as i is less than 10. The break statement within the if block halts the loop's execution when i reaches 7.


Example 3: break in a nested loop

// This demonstrates breaking out of an inner loop
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (j === 1) {
      break; // Exits only the inner loop
    }
    console.log(`i=${i}, j=${j}`);
  }
}

Explanation

In this nested loop example, the break statement only terminates the inner loop when j is 1. The outer loop continues its execution.


Example 4: Basic continue in a for loop

// This loop will skip an iteration
for (let i = 0; i < 5; i++) {
  if (i === 2) {
    continue; // Skip the rest of the loop body for this iteration
  }
  console.log(i); // Logs 0, 1, 3, 4
}

Explanation

When i is equal to 2, the continue statement is executed. This causes the current iteration to be skipped, and the loop proceeds with the next value of i.


Example 5: continue in a while loop

// This loop will skip logging an odd number
let i = 0;
while (i < 5) {
  i++;
  if (i % 2 !== 0) {
    continue; // Skip to the next iteration if i is odd
  }
  console.log(i); // Logs 2, 4
}

Explanation

This while loop increments i at the beginning of each iteration. The continue statement causes the console.log(i) to be skipped for odd numbers.


Example 6: Using continue to filter

// This loop processes only specific array elements
const data = [10, 20, 30, 40, 50];
for (let i = 0; i < data.length; i++) {
  if (data[i] <= 20) {
    continue; // Skip elements less than or equal to 20
  }
  console.log(data[i]); // Logs 30, 40, 50
}

Explanation

The continue statement in this example acts as a filter. It skips the processing of array elements that do not meet the specified condition.


Example 7: break with a label

// Breaking out of a labeled outer loop
outerLoop: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      break outerLoop; // Exits both loops
    }
    console.log(`i=${i}, j=${j}`);
  }
}

Explanation

By using a label (outerLoop), the break statement can terminate the execution of the outer loop from within the inner loop, providing more control over nested loops.