JavaScript Tutorial/Statement/Break
Break a while statement
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var x = 0;
while (x < 10)
{
x++;
document.write(x);
break;
document.write("Never seen!");
}
// -->
</script>
</head>
<body>
</body>
</html>
Break statement in a if statement
<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
var x = 0, breakat;
breakat = prompt("Pick a number between 1 and 10 to break at", "");
while (x < 10) {
if (x == breakat)
break;
}
document.write(x);
x++;
}
// -->
</script>
</head>
<body>
</body>
</html>
break statement syntax
The keyword break exits out of loop structures and switch statement.
When a label is used, JavaScript completely breaks out of the area designated by label.
To label a statement, place the label name followed by a colon (:).
Labels are useful when working with nested loops.
<html>
<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>
</html>