Visit Mozilla.org

Core JavaScript 1.5 Guide:Loop Statements:break Statement

From MDC


[edit] break Statement

Use the break statement to terminate a loop, switch, or label statement.

  • When you use break without a label, it terminates the innermost enclosing while, do-while, for, or switch immediately and transfers control to the following statement.
  • When you use break with a label, it terminates the specified labeled statement.

The syntax of the break statement looks like this:

  1. break;
  2. break label;

The first form of the syntax terminates the innermost enclosing loop or switch; the second form of the syntax terminates the specified enclosing label statement.

Example
The following example iterates through the elements in an array until it finds the index of an element whose value is theValue:

for (i = 0; i < a.length; i++) {
   if (a[i] == theValue)
      break;
}

« Previous Next »