-
Notifications
You must be signed in to change notification settings - Fork 2
JS Break
The break statement terminates the current loop, switch
, or label
statement and transfers program control to the statement following the terminated statement.
break;
If the break statement is used in a labeled statement, the syntax is as follows:
break labelName;
The following function has a break statement that terminates the while
loop when i is 3, and then returns the value 3 * x.
function testBreak(x) {
var i = 0;
while (i < 6) {
if (i == 3) {
break;
}
i += 1;
}
return i * x;
}
🚀 Run Code
In the following example, the counter is set up to count from 1 to 99; however, the break statement terminates the loop after 14 counts.
for (var i = 1; i < 100; i++) {
if (i == 15) {
break;
}
}
🚀 Run Code
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links