break
statement
JS Home <<
JS Reference <<
break
Break from the current statement flow or end the current loop.
Description
Ends the current for
, while
, do....while
loop or switch
or label
statement and passes control to the next statement.
Syntax
Signature | Description |
---|---|
break [label]; | Ends the current for , while , do....while loop or switch or label statement and passes control to the next statement. |
Parameters
Parameter | Description |
---|---|
label | An optional identifier associated with a label statement.
|
Examples
The code below shows an example of the break
statement.
// execute the loop until break.
for (var i=0; i<4; i++) {
if (i == 2) {
break;
}
alert('The i variable is = ' + i);
}
alert('left for loop');
The code below shows an example of the break
and label
statements.
// execute the loop until loop condition is false.
for (var i=1; i<3; i++) {
atLabel:
for (var k=1; k<3; k++) {
if (k == 2) {
break atLabel;
}
alert('The i variable is = ' + i + ' and k variable is = ' + k);
}
}
alert('left for loop');
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 4 - While and Do....While Loops