Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

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

for (i = 0; i <10; i ++)
{
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:

for (i=0;i<10;i++)
{
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:

Examples

for (i=0;i<=10;i++)
{
if (i==3) continue;
x=x + "The number is " + i + "<br>";
}

try it"


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:

label:
statements

break and continue statements are only able to jump statement code block.

grammar:

break labelname ;

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

cars=["BMW","Volvo","Saab","Ford"];
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"