The break and continue Statements
The break statement exits the loop. The continue statement exits the current loop.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var num = 0;
for (var i=1; i < 10; i++) {
if (i % 5 == 0) {
break;
}
num++;
}
document.writeln(num); //4
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var num = 0;
for (var i=1; i < 10; i++) {
if (i % 5 == 0) {
continue;
}
num++;
}
document.writeln(num); //8
</script>
</head>
<body>
</body>
</html>
breaks to outer most loop
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var num = 0;
outermost:
for (var i=0; i < 10; i++) {
for (var j=0; j < 10; j++) {
if (i == 5 && j == 5) {
break outermost;
}
num++;
}
}
document.writeln(num); //55
</script>
</head>
<body>
</body>
</html>
continue to outer most level:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var num = 0;
outermost:
for (var i=0; i < 10; i++) {
for (var j=0; j < 10; j++) {
if (i == 5 && j == 5) {
continue outermost;
}
num++;
}
}
document.writeln(num); //95
</script>
</head>
<body>
</body>
</html>