JavaScript Break and Continue statements
statement is used to break out of the loop.
continue for skipping a loop iteration.
Break statement
We have seen the break statement in the previous chapters of this tutorial. It breaks out of switch () statement.
break statement can be used out of the loop.
After the break statement out of the loop will continue to execute code (if any) after the cycle:
Examples
{
if (i == 3)
{
break;
}
x = x + "The number is" + i + "<br>";
}
try it"
Since this statement only if a line of code, so you can omit the curly brackets:
{
if (i==3) break;
x=x + "The number is " + i + "<br>";
}
Continue statement
continue statement break loop iteration, if there is a specified condition, and then continue to the next iteration of the loop. This example skips the value 3:
JavaScript tags
As you can see in the switch statement in the chapter, JavaScript statements can be labeled.
To mark JavaScript statement, followed by a colon before the statement:
statements
break and continue statements are only able to jump statement code block.
grammar:
continue labelname ;
continue statement (with or without label references) can only be used in a loop.
break statement (with no label references), it can only be used in loops or switch.
By label references, break statement can be used to jump out any JavaScript code block:
Examples
list:
{
document.write(cars[0] + "<br>");
document.write(cars[1] + "<br>");
document.write(cars[2] + "<br>");
break list;
document.write(cars[3] + "<br>");
document.write(cars[4] + "<br>");
document.write(cars[5] + "<br>");
}
try it"