JavaScript Break and Continue

JavaScript Break and Continue Statements

Learn JavaScript Break and Continue Statements with Examples.

JavaScript Break and Continue Statements

In JavaScript, break and continue are control flow statements that are primarily used within loops. They give developers precise control over loop execution by allowing them to either stop a loop completely or skip specific iterations based on defined conditions.

Understanding how and when to use these statements is essential for writing efficient, readable, and well-structured JavaScript code, especially when working with complex looping logic.

JavaScript break Statement

The break statement is used to immediately terminate a loop. When JavaScript encounters a break, it exits the loop instantly, regardless of whether the loop condition is still true, and continues execution with the statement that follows the loop.

This statement is commonly used when a desired condition has been met and continuing the loop would be unnecessary or inefficient. Using break can significantly improve performance when searching or validating data within loops.

The break statement can be used inside: for, while, do...while loops, as well as switch statements.

JavaScript continue Statement

The continue statement is used to skip the current iteration of a loop and immediately proceed to the next iteration. Unlike break, it does not terminate the loop.

When JavaScript executes continue, it ignores any remaining code inside the loop for the current iteration and re-evaluates the loop condition for the next cycle.

The continue statement is particularly useful when you want to exclude specific values or conditions from processing while allowing the loop to continue running normally.

Syntax Overview

break;

continue;
    

Both break and continue are typically used inside conditional statements such as if blocks within loops to control the execution flow in a clean and logical way.

JavaScript break Statement Examples

The break statement is used when you want to exit a loop immediately after a specific condition is satisfied. Once JavaScript encounters break, the loop stops executing entirely, and program control moves to the first statement after the loop.

This behavior is especially useful in scenarios such as searching for a value, validating input, or preventing unnecessary iterations once the required result has been achieved.

Example 1: Using break inside a for Loop

In this example, a for loop starts counting from 1 and is set to run until it reaches 10. During each iteration, the current value of i is checked. When i becomes equal to 5, the break statement is executed.

As a result, the loop stops immediately, and no further numbers are processed or printed to the console.

JavaScript Example

for (let i = 1; i <= 10; i++) {
    if (i === 5) {
        break;
    }
    console.log(i);
}
        
Try it Yourself ยป

The loop prints values from 1 to 4. When i reaches 5, the break statement terminates the loop, preventing any further execution.

Example 2: Using break inside a while Loop

This example demonstrates how break can be used inside a while loop that would otherwise run indefinitely. The loop condition is set to true, creating a potentially infinite loop.

Inside the loop, the variable num is logged to the console. When num reaches the value 3, the break statement is triggered, immediately stopping the loop.

JavaScript Example

let num = 1;

while (true) {
    console.log(num);
    if (num === 3) {
        break;
    }
    num++;
}
        
Try it Yourself ยป

Although the loop condition is always true, the break statement provides a controlled exit point, ensuring the loop stops safely once the desired condition is met.

โœ” The break statement helps improve both performance and code readability by preventing unnecessary loop iterations once the required outcome is achieved.

JavaScript continue Statement Examples

The continue statement is used to skip the current iteration of a loop and move directly to the next iteration. The loop itself does not stop; only the remaining code inside the current iteration is skipped.

Example 1: Using continue inside a for Loop

In this example, the loop runs from 1 to 5. When the value becomes 3, the continue statement skips that iteration and moves to the next value.

JavaScript Example

for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        continue;
    }
    console.log(i);
}
        
Try it Yourself ยป

The number 3 is skipped, but the loop continues executing for the remaining values.

Example 2: Using continue inside a while Loop

This example prints numbers from 1 to 6, but skips all even numbers using the continue statement.

JavaScript Example

let num = 0;

while (num < 6) {
    num++;

    if (num % 2 === 0) {
        continue;
    }

    console.log(num);
}
        
Try it Yourself ยป

Whenever an even number is encountered, the loop skips the console.log statement and continues with the next iteration.

โœ” Use continue when you want to ignore specific values while still allowing the loop to complete its full execution.

Difference Between break and continue in JavaScript

Although break and continue are both control flow statements used within loops, they serve very different purposes. Understanding the distinction between them is essential for writing clean, efficient, and predictable JavaScript code. The table below summarizes the key differences.

Feature break continue
Primary purpose Immediately exits the loop Skips the current iteration
Loop execution Loop stops and does not continue Loop continues with the next cycle
Condition re-evaluation Loop condition is not checked again Loop condition is checked for the next iteration
Typical use case Stop looping once a required condition is met Ignore specific values while continuing the loop
Effect on remaining code in loop Remaining loop code is never executed Remaining code is skipped only for that iteration
Use in switch statements Used to exit a case or switch block Not applicable in switch statements

โœ” In summary, use break when you need to terminate a loop entirely, and use continue when you want to skip specific iterations while allowing the loop to run to completion.

Frequently Asked Questions (FAQs)

What is the break statement in JavaScript?

The break statement is a control flow statement used to immediately terminate a loop or exit a switch case. When JavaScript encounters break, it stops executing the loop and continues with the first statement that follows it.

What is the continue statement in JavaScript?

The continue statement skips the remaining code in the current loop iteration and immediately moves execution to the next iteration. Unlike break, it does not terminate the loop.

What is the difference between break and continue in JavaScript?

The break statement exits a loop entirely, while the continue statement only skips the current iteration and allows the loop to continue running.

Can break be used inside a switch statement?

Yes. The break statement is commonly used in switch statements to exit a matched case and prevent execution from falling through to subsequent cases.

When should I use continue instead of break?

Use continue when you want to ignore specific values or conditions but still allow the loop to process the remaining iterations. This is common when filtering data or skipping unwanted cases.