About the Break Statement

The break statement causes execution within a switch, while, do/while, for, or for/in statement to terminate immediately.

See About the Switch Statement.

See About the While statement.

See About the Do/While Statement.

See About the For Statement.

See About the For/In Statement.

Break has a very simple syntax:

break ;

The following example adds all numbers in list until it encounters the first negative value. When a negative value is encountered, the loop execution is terminated by break.

var total=0,n;
for(n=0; list[n]!=null; n++) {
 if(list[n]<0)
  break;
 total+=list[n];
}

If you have nested loops, only the loop that immediately contains the break statement is terminated. For example, the break statement in the following code terminates only the innermost loop.

for(col=0; list[col]!=null; col++) {
 for(row=0; list[col][row]!=null; row++) {
  if(list[col][row]<0)
    break;
  // execute other statements
 }
}