Break to a label in JavaScript

Description

The following code shows how to break to a label.

Example


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
forLoop1:<!--from w ww . ja v a 2s .com-->
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>
</body>
</html>

Click to view the demo

The code above generates the following result.

Break to a label in JavaScript
Home »
  Javascript Tutorial »
    Statement »
      Break Continue
Javascript Tutorial Break Continue
Break out of a while statement in JavaScrip...
Break to a label in JavaScript
Continue a while loop in JavaScript
Continue statement in a if statement in Jav...
Continue to a label in JavaScript
Put Break statement in a if statement in Ja...
Use Mod operator to check even number in Ja...
Use break statement to jump to outer loop i...
Use continue statement to jump to outer mos...