JS Continue
Introduction
In JavaScript, control flow is how we direct the execution of our code. We use statements like if, else, for, while, and switch to control the sequence of operations. One powerful technique is called “Continue,” which allows us to skip ahead through a series of statements without executing the next one. This is incredibly useful for creating more concise and readable code, especially when dealing with loops or complex conditional logic. Let's dive into how to use Continue effectively.
The Basics of Continue
The continue statement skips the rest of the current iteration of a loop and jumps directly to the next iteration. It’s a way to quickly move on to the next step in a loop without processing the current one. It’s important to note that continue only affects the current iteration of a loop.
Syntax
The continue statement is placed directly after the statement it's intended to skip. Here's the basic syntax:
continue;
Practical Examples
Let's look at some practical examples to illustrate how continue works:
Example 1: Simple Loop
for (let i = 0; i < 5; i++) {
if (i % 2 === 0) {
continue; // Skip even numbers
}
console.log(i);
}
// Output:
// 1
// 3
// 5
// 0
// 2
In this example, the for loop iterates through numbers 0 to 4. The if condition checks if the number is even. If it is, the continue statement is executed, skipping the rest of the loop body for that iteration and moving to the next iteration. Only odd numbers are printed.
Example 2: Loop with a Conditional
for (let i = 0; i < 10; i++) {
if (i % 3 === 0) {
continue;
}
console.log(i);
}
// Output:
// 1
// 2
// 4
// 7
// 10
Here, the loop iterates through numbers 0 to 9. The if condition checks if the number is divisible by 3. If it is, the continue statement is executed, skipping the rest of the loop body for that iteration. Only numbers divisible by 3 are printed.
Example 3: Using Continue in a Function
function printNumbers(numbers) {
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
continue;
}
console.log(numbers[i]);
}
}
printNumbers([1, 2, 3, 4, 5, 6]); // Output: 1 3 5
In this example, the printNumbers function iterates through an array of numbers. The if condition checks if the number is even. If it is, the continue statement is executed, skipping the rest of the loop body for that iteration. Only odd numbers are printed.
continue vs. break
It's important to distinguish continue from break.
continueskips the current iteration of a loop.breakimmediately exits the entire loop.
for (let i = 0; i < 10; i++) {
if (i === 5) {
break; // Exit the loop when i is 5
}
console.log(i);
}
// Output:
// 0
// 1
// 2
// 3
// 4
Summary and Key Takeaways
The continue statement is a valuable tool for streamlining JavaScript code. It allows you to efficiently skip iterations of loops, making your code more concise and easier to read. Understanding how to use continue effectively will significantly improve your ability to write clean and efficient JavaScript programs. Don't underestimate its power – it's a fundamental concept for mastering control flow.
💡 Tip: Consider using continue when you need to process a subset of elements within a loop without processing the rest of the elements. It can often lead to more readable and maintainable code.

