JavaScript tutorial:
break statement

 

The break statement is used to terminate the current loop, or if in conjunction with a label, terminates the associated statement.

Syntax

break [label];

The optional label argument specifies the label of the statement you are breaking from.

Example

You typically use the break statement in switch statements and while, for, for...in, or do...while loops. You most commonly use the label argument in switch statements, but it can be used in any statement, whether simple or compound.

Executing the break statement exits from the current loop or statement, and begins script execution with the statement immediately following.

The following example illustrates the use of the break statement:

function BreakTest(breakpoint)
{
var i = 0;
while (i < 100)
{
if (i == breakpoint)
break;
i++;
}
return(i);
}
document.write(prompt("Please enter a number between 0 and 100: ","10"));

To run the code above, paste it into JavaScript Editor, and click the Execute button.

See also: continue Statement, do...while Statement, for Statement, for...in Statement, Labeled Statement, while Statement