Table of Contents
The continue statement is a control flow statement that is used within loops to skip the current iteration and move to the next iteration. It is commonly used when we want to bypass certain iterations and continue with the next iteration based on a specific condition.
Using the continue statement in loops
The continue statement is typically used within loops, such as the for loop or the while loop. When the continue statement is encountered, it immediately stops the execution of the current iteration and proceeds to the next iteration.
Here’s an example of using the continue statement in a for loop:
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}In the above example, when the value of i is 3, the continue statement is triggered, skipping the console.log(i) statement, and moving to the next iteration.
Skipping iterations with continue
The continue statement allows us to skip certain iterations based on specific conditions. This can be useful when we want to filter out certain elements or perform specific actions only for certain iterations.
Let’s consider an example where we want to print all even numbers from 1 to 10 using a for loop:
for (let i = 1; i <= 10; i++) {
if (i % 2 !== 0) {
continue;
}
console.log(i);
}In the above example, the continue statement is used to skip the odd numbers, allowing only the even numbers to be printed.
The continue statement can be applied in various scenarios to control the flow of execution. Here are a few examples:
Skipping specific items in an array
const fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi'];
for (let i = 0; i < fruits.length; i++) {
if (fruits[i] === 'banana') {
continue;
}
console.log(fruits[i]);
}In this example, the continue statement skips the iteration when the current fruit is ‘banana’, preventing it from being printed.
Filtering elements from an array
const numbers = [10, 20, 30, 40, 50];
const filteredNumbers = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] < 30) {
continue;
}
filteredNumbers.push(numbers[i]);
}
console.log(filteredNumbers);In this example, the continue statement is used to skip the numbers less than 30, resulting in a filtered array that only contains numbers greater than or equal to 30.
Common mistakes when using continue
While using the continue statement, there are a few common mistakes to be aware of:
Forgetting to update the loop variable
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue;
}
console.log(i);
i++; // Incorrectly updating the loop variable
}In this example, the loop variable i is incorrectly updated within the loop body. This results in skipping some iterations and not printing all the expected values.
Misplacing the continue statement
for (let i = 0; i < 5; i++) {
console.log(i);
continue; // Misplaced continue statement
}In this example, the continue statement is placed at the end of the loop body instead of within an if statement. As a result, it has no effect on the loop execution.
Benefits of using the continue statement
The continue statement offers several benefits in JavaScript programming:
- Enhanced control flow: The continue statement provides finer control over loop iterations, allowing developers to skip unnecessary iterations based on specific conditions.
- Improved code readability: By using the continue statement appropriately, code logic becomes more readable and expressive as it reflects the intention of skipping iterations clearly.
- Efficient code execution: The continue statement helps optimize code execution by avoiding unnecessary calculations or operations for certain iterations.
- Reduced complexity: With the ability to skip iterations, complex filtering or processing logic can be simplified, resulting in cleaner and more manageable code.
Conclusion
In JavaScript, the continue statement is a powerful tool for controlling loop execution. It allows developers to skip iterations and move to the next iteration based on specific conditions. By using the continue statement effectively, you can enhance the control flow of your code and improve its readability. Remember to be mindful of common mistakes and leverage the benefits offered by the continue statement to write more efficient and concise JavaScript programs.

