Break statement
Interrupts the current loop, or uses it with the label to break the associated statement.
break [Label];
The optional label parameter specifies the label of the statement at the breakpoint.
Description
Break statements are typically used in switch statements and while, for, for...in, or do...while loops. The most common is to use the label parameter in a switch statement, but it can be used in any statement, whether it is a simple statement or a compound statement.
Executing a break statement exits the current loop or statement and begins the execution of the statement immediately following the script
Apply break in For loop
<script language= ' JavaScript ' >
<!--
FORLOOP1:
for (var counter1 = 1; counter1 <= 5; counter1++)
{
for (var counter2 = 1; counter2 <= 5; counter2++)
{
document.write ("counter1=", counter1);
document.write ("counter2=", Counter2, "<BR>");
if (Counter2 = 3)
Break
if (Counter1 = 3)
Break ForLoop1;
}
}
document.write ("All done!");
-->
</SCRIPT>
In the While loop
<script language= "JavaScript" >
<!--
var x = 0;
while (x < 10)
{
x + +;
document.write (x);
Break
document.write ("Never seen!");
}
-->
</script>
<script language= "JavaScript" >
<!--
var x = 0, Breakat;
Breakat = Prompt ("Pick a number between 1", "" ");
while (x < 10) {
if (x = = Breakat)
Break
}
document.write (x);
x + +;
}
-->
</script>
There are two special statements that can be used within a loop: The break and Continue,break commands terminate the loop's operation, and then continue to execute the code after the loop (if there is code after the loop).