JavaScript break Statement

Definition and usage

The break statement exits a switch statement or loop (for, for...in, while, do...while).

When the break statement is used with a switch statement, it exits the switch block. This will stop executing more code and/or case tests within the block.

When the break statement is used within a loop, it breaks out of 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" any JavaScript code block (see the "More examples" section below).

Note:If a label is not referenced, the break statement can only be used within a loop or switch.

Example

In this example, we use the for loop in conjunction with the break statement.

Loop through a block of code, but exit the loop when the variable i equals "3":

var text = ""
var i;
for (i = 0; i < 5; i++) {
  if (i === 3) {
    break;
  }
  text += "The number is " + i + "<br>";
}

try it yourself

More TIY examples are available at the bottom of the page.

Syntax

break;

Use optional labels to reference:

break labelname;

Technical details

JavaScript version: ECMAScript 1

More examples

Example

In this example, we use the while loop in conjunction with the break statement.

Loop through a block of code, but exit the loop when the variable i equals "3":

var text = "";
var i = 0;
while (i < 5) {
  text += "<br>The number is " + i;
  i++;
  if (i === 3) {
    break;
  }
}

try it yourself

Example

Exit the switch block to ensure that only one case 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;
}

try it yourself

Example

Use a labeled break statement to "exit" a JavaScript code 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>"; 
}

try it yourself

Example

Use a labeled break statement to "exit" nested for loops with a label reference:

var text = "";
var i, j;
Loop1: // The first for loop is marked as "Loop1"
for (i = 0; i < 3; i++) {
text += "<br>" + "i = " + i + ", j = ";
  Loop2: // The second for loop is marked as "Loop2"
  for (j = 0; j < 5; j++) {
    if (j === 2) {
      break Loop1;
    }
    document.getElementById("demo").innerHTML = text += j + " ";
  }
}

try it yourself

browser support

statement Chrome IE Firefox Safari Opera
break support support support support support

related pages

JavaScript Tutorial:JavaScript Break and Continue

JavaScript Tutorial:JavaScript For Loop

JavaScript Tutorial:JavaScript While Loop

JavaScript Tutorial:JavaScript Switch

JavaScript Reference Manual:JavaScript continue Statement

JavaScript Reference Manual:JavaScript for Statement

JavaScript Reference Manual:JavaScript while Statement

JavaScript Reference Manual:JavaScript switch Statement