DEV Community

Web Easly
Web Easly

Posted on

JavaScript Break and Continue Statements

JavaScript provides two important control flow statements: break and continue. These statements offer a way to manipulate the flow of a loop, enhancing the control and efficiency of your code. In this article, we will delve into the purposes and usage of these statements, accompanied by examples to illustrate their functionality.

The Break Statement

The break statement is used to terminate a loop prematurely, allowing you to exit the loop based on a certain condition. This is particularly useful when you want to stop the execution of a loop once a specific condition is met. Let's consider an example using a for loop:

for (let i = 1; i <= 10; i++) {
  if (i === 5) {
    console.log("Loop interrupted at i =", i);
    break;
  }
  console.log(i);
}
Enter fullscreen mode Exit fullscreen mode

In this example, the loop will print numbers from 1 to 4, but when i becomes 5, the break statement will be triggered, and the loop will terminate. The output will be:

1
2
3
4
Loop interrupted at i = 5
Enter fullscreen mode Exit fullscreen mode

The Continue Statement

On the other hand, the continue statement is used to skip the rest of the code inside a loop for the current iteration and proceed to the next iteration. This can be beneficial when you want to skip certain elements in an array or perform conditional skips within a loop. Here's an example using a while loop:

let i = 0;

while (i < 5) {
  i++;
  if (i === 3) {
    console.log("Skipped at i =", i);
    continue;
  }
  console.log(i);
}
Enter fullscreen mode Exit fullscreen mode

In this case, when i equals 3, the continue statement is triggered, and the loop skips the code below it for that iteration. The output will be:

1
2
Skipped at i = 3
4
5
Enter fullscreen mode Exit fullscreen mode

Practical Examples

Let's explore more practical scenarios. Consider a loop that iterates through an array of numbers and breaks when a specific condition is met:

const numbers = [10, 20, 30, 40, 50];

for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] === 30) {
    console.log("Condition met at index", i);
    break;
  }
  console.log(numbers[i]);
}
Enter fullscreen mode Exit fullscreen mode

In this example, the loop will print numbers until it encounters 30 in the array. The output will be:

10
20
Condition met at index 2
Enter fullscreen mode Exit fullscreen mode

In another scenario, let's use the continue statement to skip odd numbers in a loop:

for (let i = 1; i <= 10; i++) {
  if (i % 2 !== 0) {
    console.log("Skipped odd number:", i);
    continue;
  }
  console.log(i);
}
Enter fullscreen mode Exit fullscreen mode

Here, the loop prints even numbers and skips the odd ones, resulting in the output:

2
Skipped odd number: 3
4
Skipped odd number: 5
6
Skipped odd number: 7
8
Skipped odd number: 9
10
Enter fullscreen mode Exit fullscreen mode

In conclusion, the break and continue statements are powerful tools in JavaScript, offering flexibility in controlling the flow of loops. Whether you need to prematurely exit a loop or skip specific iterations, these statements can greatly enhance the efficiency and readability of your code.

Top comments (0)