JavaScript break
Statement
JavaScript Statements Reference
Example
This example demonstrates the use of the break
statement in a loop.
The loop block exits when the variable i
is "3":
var text = "";
var i;
for (i = 0; i < 5; i++) {
if (i == 3) {
break;
}
text += "The number is " + i + "<br>";
}
text output result:
The number is 0
The number is 1
The number is 2
More examples are included at the bottom of this article.
Definition and Usage
The break
statement is used to exit a switch
statement or a loop (for, for...in, while, do...while).
When used in a switch
statement, it exits the switch
block, stopping the execution of any more code.
When used in a loop, it terminates the loop and continues executing the code after the loop (if any).
The break
statement can also be used with an optional label reference to exit a block of code. (See "More Examples" below).
Note: The break
statement (without label reference) can only be used inside loops or a switch
.
Browser Support
Statement | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
break | Yes | Yes | Yes | Yes | Yes |
Syntax
With optional label reference:
Technical Details
| JavaScript Version: | 1.0. JavaScript 1.2 supports optional labels. | | --- | --- |
More Examples
Example
This example uses the break
statement in a while
loop.
The loop block exits when i
equals "3":
var text = "";
var i = 0;
while (i < 5) {
text += "<br>The number is " + i;
i++;
if (i == 3) {
break;
}
}
text output result:
The number is 0
The number is 1
The number is 2
Example
Exiting a switch
block to ensure only one case
statement is executed:
var day;
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
}
day output result:
var d = new Date().getDay();
switch (d) {
case 0:
x = "Sunday";
break;
case 1:
x = "Monday";
break;
case 2:
x = "Tuesday";
break;
case 3:
x = "Wednesday";
break;
case 4:
x = "Thursday";
break;
case 5:
x = "Friday";
break;
case 6:
x = "Saturday";
break;
}
document.write(x);
Example
Using the break
statement with a label reference to exit a block:
var cars = ["BMW", "Volvo", "Saab", "Ford"];
var text = "";
list: {
text += cars[0] + "<br>";
text += cars[1] + "<br>";
text += cars[2] + "<br>";
break list;
text += cars[3] + "<br>";
}
text output result:
BMW
Volvo
Saab
Example
Using the break
statement with a label reference to exit nested loops:
var text = "";
var i, j;
Loop1: // First loop label "Loop1"
for (i = 0; i < 3; i++) {
text += "<br>" + "i = " + i + ", j = ";
Loop2: // Second loop label "Loop2"
for (j = 10; j < 15; j++) {
if (j == 12) {
break Loop2;
}
document.getElementById("demo").innerHTML = text += j + " ";
}
}
text output result:
i = 0, j = 10 11
i = 1, j = 10 11
i = 2, j = 10 11
Related Pages
JavaScript Tutorial: JavaScript Break and Continue
JavaScript Tutorial: JavaScript Loops
JavaScript Tutorial: JavaScript While Loop
JavaScript Tutorial: JavaScript continue Statement
JavaScript Reference: JavaScript for Statement
JavaScript Reference: JavaScript while Statement