Line At A Time
Lesson 22: Continue
Continue Skip one trip around a loop with continue.
1for (let i = 1; i <= 5; i++) {
2 if (i === 3) {
3 continue;
4 }
5 console.log(i);
6}
Terminal
$ node main.js
1
2
4
5
Every line, explained
for (let i = 1; i <= 5; i++) {
This counter starts at 1, and <= means "less than or equal", so the loop counts 1, 2, 3, 4, 5.
if (i === 3) {
if checks the condition inside its round brackets. If the condition is true, JavaScript runs the code between { and }; if it is false, it skips that code completely. We single out the moment the counter hits 3.
continue;
continue skips the REST of this trip and jumps straight to the next count. When i is 3, the console.log below never happens, so 3 is missing from the output.
}
This } closes the if block.
console.log(i);
console.log(...) prints whatever is inside the round brackets to the terminal, then moves to a new line. console is the terminal itself, log is the tool that writes to it, joined with a dot. Prints 1, 2, 4 and 5, but not 3, because continue skipped past this line that time.
}
This } closes the for loop.