Terminates the current loop, or if in conjunction with a label, terminates the associated statement.
break [label]; |
Arguments
- label
-
Optional. Specifies the label of the statement you are breaking from.
Remarks
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 causes the program flow to exit the current loop or statement. Program flow resumes with the next statement immediately following the current loop or statement.
Example 1
The following example illustrates the use of the break statement.
В | Copy Code |
---|---|
function breakTest(breakpoint){ var i = 0; while (i < 100) { if (i == breakpoint) break; i++; } return(i); } |
Example 2
The following example illustrates the use of the labeled break statement.
В | Copy Code |
---|---|
function nameInDoubleArray(name, doubleArray) { var i, j, inArray; inArray = false; mainloop: for(i=0; i<doubleArray.length; i++) for(j=0; j<doubleArray[i].length; j++) if(doubleArray[i][j] == name) { inArray = true; break mainloop; } return inArray; } |