break "jumps out" of a loop or a switch immediately, even if the loop's condition is still true. Use it when you have found what you were looking for and there is no reason to keep going.
When JavaScript reaches break inside a loop, the loop ends on the spot. No more iterations run, and the program continues with the first statement after the loop:
for (let i = 1; i <= 10; i++) {
if (i === 4) {
break;
}
console.log(i);
}
console.log("Loop finished");
When i reaches 4, break fires before the console.log, so 4 is never printed, and neither is anything after it. break almost always sits inside an if: "if this happens, stop".
The most common use: search a list and stop as soon as you find a match.
const names = ["Riley", "Jordan", "Sam", "Casey", "Drew"];
let foundAt = -1;
for (let i = 0; i < names.length; i++) {
console.log("Checking " + names[i]);
if (names[i] === "Sam") {
foundAt = i;
break;
}
}
console.log("Found at position " + foundAt);
Casey and Drew were never checked. On a list of a million items, stopping early can save a lot of work.
foundAt to -1 before the loop is a common pattern. If the loop finishes without a match, -1 signals "not found", since no real position is negative.
Sometimes a loop is written with while (true) on purpose, and break is the only way out:
let attempts = 0;
while (true) {
attempts++;
const roll = Math.floor(Math.random() * 20) + 1;
if (roll === 20) {
console.log("Natural 20 on attempt " + attempts);
break;
}
}
while (true) loop without a reachable break freezes the page. Make sure the break condition can actually happen: roll === 21 on a 20-sided die never would.
You met break in the Switch lesson. It does the same job there: it exits the switch block after a matching case runs. Without it, execution "falls through" into the following cases, even though their values do not match.
switch (dayNum) {
case 0:
day = "Sunday";
break; // leave the switch here
case 1:
day = "Monday";
break;
// ...
}
switch sits inside a loop, a break in one of its cases exits only the switch, not the loop. The loop carries on with its next pass. To leave the loop from inside the switch, use a label (below).
In nested loops, break exits only the innermost loop it sits in. The outer loop keeps going:
for (let row = 1; row <= 3; row++) {
for (let col = 1; col <= 3; col++) {
if (col === 2) break;
console.log(row + "," + col);
}
}
A label gives a name to a statement or block, so break (and continue) can refer to it. A label is a name followed by a colon, placed right before the statement:
labelname: statement;
labelname: {
statements
}
With a label, break labelname; exits the labeled statement instead of just the innermost loop. Compare these two, which differ only in which loop the break targets:
let text = "";
loop1: for (let j = 1; j < 5; j++) {
loop2: for (let i = 1; i < 5; i++) {
if (i === 3) { break loop1; } // exit BOTH loops
text += i;
}
}
console.log(text);
let text = "";
loop1: for (let j = 1; j < 5; j++) {
loop2: for (let i = 1; i < 5; i++) {
if (i === 3) { break loop2; } // exit the inner loop only
text += i;
}
}
console.log(text);
In the first, hitting i === 3 ends everything after one pass: "12". In the second, it ends only the inner loop, so the outer loop runs all 4 times, adding "12" each time. (break loop2 does the same as a plain break here, since loop2 is the innermost loop.)
Without a label, break only works inside a loop or switch. With a label, it can jump out of any code block. break and continue are the only statements that can jump out of a block like this:
const cars = ["BMW", "Volvo", "Saab", "Ford"];
let text = "";
list: {
text += cars[0] + " ";
text += cars[1] + " ";
break list; // skip the rest of the block
text += cars[2] + " ";
text += cars[3] + " ";
}
console.log(text);
loop1: in front of a loop looks confusing if you have never seen one. If you find yourself needing a label, the nested loop can often go into its own function instead, where return exits everything at once (see JS Control Flow).
What does this log?
for (let i = 0; i < 10; i++) {
if (i * i > 10) break;
console.log(i);
}
0, 1, 2, 3. At i = 4, 16 > 10 is true, so the loop stops before printing 4.A break sits inside an inner loop, which is inside an outer loop. No labels are used. What happens when it runs?
break exits only the innermost loop it is in. To stop the outer loop too, label it and use break outerLabel;.Where can a plain break (no label) be used?
if block onlyswitchbreak must be inside a loop or switch. It is often written inside an if, but that if itself has to be inside a loop or switch. Anywhere else, it is a SyntaxError.break at the first number divisible by both 7 and 5. Log that numberwhile (true) loop that breaks after five passes. Write the break before you click Runbreak loop1 example with its Try it Yourself button and confirm you get 12. Change it to break loop2 and confirm 12121212. Explain the difference in your notes