HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
  • Insights
  • Verify Certificate
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials

HTML5andCSS3.org helps developers learn modern frontend skills with practical tutorials, production-ready snippets, and fast web tools built for everyday coding.

Quick Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Copyright
  • Disclaimer

Top Tools

  • Try It Editor
  • JSON Formatter
  • CSS Minifier
  • HTML Minifier
  • Base64 Encoder / Decoder

Contact

  • business@html5andcss3.org
Newsletter

Learn at your own pace, practice with useful tools, and build websites you're proud of.

© 2011-2026 html5andcss3.org. All rights reserved.

How to Use

JavaScript Tutorial

JavaScript Introduction

What is JavaScriptJavaScript FeaturesJavaScript SyntaxHow to Add JavaScriptJavaScript OutputJavaScript Comments

JavaScript Basics

JavaScript VariablesJavaScript Data TypesJavaScript Operators

JavaScript Statement & Expression

JavaScript StatementsJavaScript ExpressionsJavaScript KeywordsJavaScript IdentifiersJavaScript Case SensitivityJavaScript SemicolonsJavaScript Code Blocks

JavaScript Conditions

JavaScript if StatementJavaScript if else StatementJavaScript else if StatementJavaScript switch Statement

JavaScript Loops

JavaScript for LoopJavaScript while LoopJavaScript do while LoopJavaScript break Statement
  1. Home
  2. /
  3. Tutorials
  4. /
  5. JavaScript Tutorial
  6. /
  7. JavaScript break Statement

JavaScript Loops

JavaScript break Statement

The JavaScript break statement is used to stop a loop before it normally ends.

When JavaScript finds a break statement inside a loop, it immediately exits the loop and continues with the code after the loop.

Example

Example

javascript

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break;
  }

  console.log(i);
}

In this example, the loop stops when i becomes 3.

What is the break Statement?

The break statement is used to terminate a loop.

It is commonly used when you want to stop repeating code after a certain condition is met.

Syntax

Syntax

javascript

break;

The break statement is usually written inside an if condition.

Example

Syntax

javascript

if (condition) {
  break;
}

break in a for Loop

You can use break inside a for loop.

Example

break in a for Loop

javascript

for (let i = 1; i <= 10; i++) {
  if (i === 5) {
    break;
  }

  console.log(i);
}

Here, the loop stops when i becomes 5.

So, the loop does not continue until 10.

break in a while Loop

The break statement can also be used inside a while loop.

Example

break in a while Loop

javascript

let i = 1;

while (i <= 10) {
  if (i === 6) {
    break;
  }

  console.log(i);
  i++;
}

Here, the loop stops when i becomes 6.

break in a do while Loop

You can also use break inside a do while loop.

Example

break in a do while Loop

javascript

let i = 1;

do {
  if (i === 4) {
    break;
  }

  console.log(i);
  i++;
} while (i <= 10);

Here, the loop stops when i becomes 4.

Why Use break?

The break statement is useful when you want to stop a loop after finding what you need.

Example

Why Use break?

javascript

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

for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] === 30) {
    console.log("Number found");
    break;
  }
}

In this example, the loop stops after finding 30.

There is no need to check the remaining numbers.

break with Arrays

The break statement is commonly used while searching through arrays.

Example

break with Arrays

javascript

let fruits = ["Apple", "Banana", "Mango", "Orange"];

for (let i = 0; i < fruits.length; i++) {
  if (fruits[i] === "Mango") {
    console.log("Mango found");
    break;
  }

  console.log(fruits[i]);
}

When "Mango" is found, the loop stops.

break with Strings

You can also use break while checking characters in a string.

Example

break with Strings

javascript

let name = "John";

for (let i = 0; i < name.length; i++) {
  if (name[i] === "h") {
    break;
  }

  console.log(name[i]);
}

Here, the loop stops when it finds "h".

break in switch Statement

You have already seen break in the switch statement.

Example

break in switch Statement

javascript

let day = 1;

switch (day) {
  case 1:
    console.log("Monday");
    break;

  case 2:
    console.log("Tuesday");
    break;

  default:
    console.log("Invalid day");
}

In a switch statement, break stops JavaScript from running the next case.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript break Statement</title>
</head>
<body>

  <h1>JavaScript break Statement</h1>

  <p id="result"></p>

  <script>
    let result = "";

    for (let i = 1; i <= 10; i++) {
      if (i === 6) {
        break;
      }

      result += "Number: " + i + "<br>";
    }

    document.getElementById("result").innerHTML = result;
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try changing this condition:

Try It Yourself

javascript

if (i === 6)

For example:

Try It Yourself

javascript

if (i === 4)

Then check where the loop stops.

Important Points

  • The break statement is used to stop a loop.
  • When break runs, JavaScript immediately exits the loop.
  • It is commonly used with an if condition.
  • The break statement can be used inside for, while, and do while loops.
  • It is also used in switch statements.
  • Use break when you no longer need the loop to continue.

Conclusion

The JavaScript break statement is used to stop a loop before its normal ending point.

It is useful when a condition has been met and there is no need to continue repeating the code.

Understanding break helps you control loops more effectively and write cleaner JavaScript programs.

PreviousJavaScript do while Loop