English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript basic tutorial

JavaScript object

JavaScript function

JS HTML DOM

JS browser BOM

AJAX basic tutorial

JavaScript reference manual

JavaScript Break and Continue Statements

The function of the break statement is to 'exit' a loop.

The continue statement skips an iteration and starts the next iteration of the loop.

break statement

The break statement is used to terminate the current loop, switch, or labeled statement, and transfer control to the statement after the terminating statement.

When used in a loop, the break statement will interrupt the loop and continue executing code after the loop.

var text = "";
for (let i = 0; i < 6; i++) {
if (i === 3) {
   break;
}
text += "The number is " + i + "<br>";
}
Test and See‹/›

When used with the switch statement, the break statement will exit the switch block. This will stop executing more code within the block and/or perform case testing.

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;
}
Test and See‹/›

continue statement

The continue statement terminates the execution of the current iteration and continues with the next iteration of the loop.

The following example shows a for loop with a continue statement, which is executed when the value of i is3execute when:

var text = "";
for (let i = 0; i < 6; i++) {
if (i === 3) {
   continue;
}
text += "The number is " + i + "<br>";
}
Test and See‹/›

The following example shows a while loop with a continue statement, which is executed when the value of i is3execute when:

var text = "";
var i = 0;
while (i < 6) {
   i++;
   if (i === 3) {
  continue;
   }
   text += "The number is " + i + "<br>";
}
Test and See‹/›

JavaScript Labels

Labels are identifiers followed by a colon (:), which applies to a statement or code block.

label:
statements

The break statement can be used to 'exit' from loops, switches, or labeled statements.

break labelname;

The following code uses a break statement with a labeled block to 'exit' from a JavaScript code block:

outer_block: {
  inner_block: {
 document.writeln('1');
 break outer_block;// Exit from inner_block and outer_block
 document.writeln('-(' // Skip
  }
  document.writeln('2'); // Skip
}
Test and See‹/›