JavaScript Break & Continue

Learn JavaScript break and continue statements with detailed explanations, syntax, examples, outputs, and real-world practical usage.

JavaScript Break & Continue

The break and continue statements are loop control statements in JavaScript. They are used to control the execution flow of loops.

These statements help developers stop loops early or skip specific iterations based on conditions.

Why Break & Continue are Important?

  • Control loop execution
  • Improve program efficiency
  • Skip unnecessary iterations
  • Stop loops when needed
  • Build smarter applications

What is break Statement?

The break statement immediately stops the loop execution. Once break runs, the loop terminates completely.

Syntax of break

break;

Basic break Example

The following example stops the loop when the value becomes 5.

for(let i = 1; i <= 10; i++){

    if(i == 5){

        break;

    }

    console.log(i);

}

Output

1

2

3

4

Detailed Explanation

  • The loop starts from 1.
  • The condition checks whether i equals 5.
  • When i becomes 5, break executes.
  • The loop stops immediately.

Using break in while Loop

break also works inside while loops.

let i = 1;

while(i <= 10){

    if(i == 6){

        break;

    }

    console.log(i);

    i++;

}

Output

1

2

3

4

5

Explanation

  • The loop continues while i is less than or equal to 10.
  • When i becomes 6, break stops execution.

What is continue Statement?

The continue statement skips the current iteration and moves directly to the next iteration.

Syntax of continue

continue;

Basic continue Example

The following example skips number 3.

for(let i = 1; i <= 5; i++){

    if(i == 3){

        continue;

    }

    console.log(i);

}

Output

1

2

4

5

Detailed Explanation

  • The loop runs from 1 to 5.
  • When i becomes 3, continue executes.
  • The current iteration is skipped.
  • The loop continues with the next value.

Using continue in while Loop

let i = 0;

while(i < 5){

    i++;

    if(i == 2){

        continue;

    }

    console.log(i);

}

Output

1

3

4

5

Explanation

  • The loop skips value 2.
  • continue jumps directly to the next iteration.

Difference Between break and continue

  • break completely stops the loop.
  • continue skips only the current iteration.
  • break exits the loop permanently.
  • continue allows the loop to continue running.

Skipping Odd Numbers

continue is commonly used to skip unwanted values.

for(let i = 1; i <= 10; i++){

    if(i % 2 != 0){

        continue;

    }

    console.log(i);

}

Output

2

4

6

8

10

Explanation

  • The condition checks odd numbers.
  • Odd numbers are skipped using continue.
  • Only even numbers are printed.

Using break to Stop Infinite Loop

break is useful for stopping infinite loops safely.

let i = 1;

while(true){

    console.log(i);

    if(i == 5){

        break;

    }

    i++;

}

Output

1

2

3

4

5

Explanation

  • while(true) creates an infinite loop.
  • break stops the loop when i becomes 5.

break in Nested Loops

break only stops the current loop.

for(let i = 1; i <= 3; i++){

    for(let j = 1; j <= 5; j++){

        if(j == 3){

            break;

        }

        console.log(i, j);

    }

}

Output

1 1

1 2

2 1

2 2

3 1

3 2

Explanation

  • The inner loop stops when j becomes 3.
  • The outer loop continues normally.

Complete Real Life Example

The following example shows a product search system using break and continue statements.

<!DOCTYPE html>

<html>

<head>

    <title>JavaScript Break Continue Example</title>

</head>

<body>

<h2>Available Products</h2>

<button onclick="showProducts()">

Show Products

</button>

<ul id="output"></ul>

<script>

function showProducts(){

    let products = [

        "Laptop",

        "Mobile",

        "Out of Stock",

        "Keyboard",

        "Mouse"

    ];

    let result = "";

    for(let i = 0; i < products.length; i++){

        if(products[i] == "Out of Stock"){

            continue;

        }

        result += "<li>" +

        products[i] +

        "</li>";

    }

    document.getElementById("output").innerHTML =

    result;

}

</script>

</body>

</html>

Output

Available Products

• Laptop
• Mobile
• Keyboard
• Mouse

Detailed Explanation of Example

  • The array stores product names.
  • The loop checks every product.
  • If a product is "Out of Stock", continue skips it.
  • Only available products are added to the list.
  • innerHTML displays products dynamically.
  • continue helps filter unwanted data.

Common Mistakes

  • Using break instead of continue accidentally.
  • Creating infinite loops without break.
  • Forgetting loop updates.
  • Writing incorrect conditions.

Best Practices

  • Use break only when necessary.
  • Use continue for skipping unwanted iterations.
  • Keep loop conditions simple.
  • Write readable logic.
  • Avoid unnecessary nested loops.

Important Notes

  • break completely stops loops.
  • continue skips current iterations only.
  • Both statements improve loop control.
  • break is useful for search operations.
  • continue is useful for filtering data.

Summary

  • break stops loop execution completely.
  • continue skips the current iteration.
  • Both statements control loops efficiently.
  • break helps exit loops early.
  • continue skips unwanted values.
  • These statements improve program performance.
  • They are widely used in real applications.