continue statement
The continue command forces
a moving to the next loop iteration.
Syntax
continue;
Example
Let's say we have an array of
numbers. Let's iterate over it
and the numbers that are divisible
by 2 will be squared and
output to the console, and the
numbers that are divisible by
3 will be cubed and
output to the console:
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
for (let elem of arr) {
let res;
if (elem % 2 == 0) {
res = elem * elem;
} else if (elem % 3 == 0) {
res = elem * elem * elem;
} else {
continue; // we move onto a new loop iteration
}
console.log(res); // executed if divisible by 2 or 3
}
See also
-
the lesson from JavaScript tutorial
detailing how to work withcontinue -
the
breakstatement
that force a loop to terminate