- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript break Statement
JavaScript Loops
JavaScript break Statement
The JavaScript
breakstatement 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
breakstatement is used to stop a loop. - When
breakruns, JavaScript immediately exits the loop. - It is commonly used with an
ifcondition. - The
breakstatement can be used insidefor,while, anddo whileloops. - It is also used in
switchstatements. - Use
breakwhen 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.
