Skip to content
Quincy Larson edited this page Aug 20, 2016 · 1 revision

JavaScript Break

Introduction

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;

MDN link | MSDN link

Examples

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

Clone this wiki locally