DEV Community

Cover image for ๐Ÿ” Skipping Iterations in JavaScript Loops: `for`, `forEach`, and Beyond
Abhinav
Abhinav

Posted on

3 2 1 1 2

๐Ÿ” Skipping Iterations in JavaScript Loops: `for`, `forEach`, and Beyond

When working with loops in JavaScript, it's common to want to skip certain iterations โ€” whether to avoid processing specific values or just to make the logic cleaner.

But not all loop types work the same way when it comes to skipping!

In this blog, weโ€™ll explore:

  • โœ… How to skip iterations using for loops
  • โŒ Why continue doesnโ€™t work in forEach
  • ๐Ÿ’ก Workarounds and alternatives
  • ๐Ÿง  Which loop to choose based on your use case

๐Ÿงช 1. Skipping Iterations in a for Loop (The Classic Way)

The continue statement is your friend here.

for (let i = 0; i < 5; i++) {
  if (i === 2) {
    continue; // Skip when i is 2
  }
  console.log(i);
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿงพ Output:

0
1
3
4
Enter fullscreen mode Exit fullscreen mode

โœ… Why it works:

The continue statement tells the loop to skip the rest of the current iteration and move to the next one.


๐Ÿšซ 2. Why continue Doesnโ€™t Work in forEach

[1, 2, 3].forEach((num) => {
  if (num === 2) {
    continue; // โŒ SyntaxError!
  }
  console.log(num);
});
Enter fullscreen mode Exit fullscreen mode

Image description
Thatโ€™s because forEach uses a callback function, and continue is only allowed inside actual loop constructs like for, while, or for...of.


โœ… 3. Skipping in forEach with return

Instead of continue, you can use return to skip the current callback execution:

[1, 2, 3, 4].forEach((num) => {
  if (num === 3) return; // Skip 3
  console.log(num);
});
Enter fullscreen mode Exit fullscreen mode

๐Ÿงพ Output:

1
2
4
Enter fullscreen mode Exit fullscreen mode

snippet-forEach

โœ… Why it works:

Using return exits the current callback function early โ€” effectively skipping that iteration.


๐Ÿ” 4. for...of โ€” The Flexible Alternative

Need more control (like both continue and break)? for...of is a great middle ground:

const nums = [10, 20, 30, 40];

for (const num of nums) {
  if (num === 30) continue;
  console.log(num);
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿงพ Output:

10
20
40
Enter fullscreen mode Exit fullscreen mode

You can also use break here if needed โ€” which you canโ€™t do with forEach.


๐Ÿง  Which Loop Should You Use?

Use Case Best Loop
Need break or continue? for, for...of
Just iterating over items? forEach (if no need to skip/break)
Transforming data? map, filter, reduce
Working with async code? for...of with await

๐Ÿ“Œ Final Thoughts

Choosing the right loop isnโ€™t just about preference โ€” itโ€™s about control. If you need to skip, short-circuit, or break, prefer for or for...of. Use forEach only when you're sure you'll process every item and donโ€™t need to interrupt the flow.


โœ๏ธ Hope this helped clarify one of those subtle gotchas in JavaScript loops!

Scale globally with MongoDB Atlas. Try free.

Scale globally with MongoDB Atlas. Try free.

MongoDB Atlas is the global, multi-cloud database for modern apps trusted by developers and enterprises to build, scale, and run cutting-edge applications, with automated scaling, built-in security, and 125+ cloud regions.

Learn More

Top comments (3)

Collapse
 
vanshul22 profile image
Vanshul Kesharwani โ€ข

Great post Abhinav!
Just to add - for...in is another option

It iterates over object keys (or array indices),
You can use both break and continue with for...in loop.

Best used for objects:

`
const obj = {a: 1, b: 2, c: 3, d: 4};

for (const key in obj) {
if (key === 'b') continue; // Skip this iteration
if (key === 'd') break; // Exit the loop
console.log(key, obj[key]);
}
// Output: a 1, c 3
`

So for...in has the same flow control as regular for and for...of loops - unlike forEach which can't use break/continue.

Collapse
 
itxitpro_llc profile image
ITXITPro LLC โ€ข

Great breakdown of loop control in JavaScript! ๐Ÿ™Œ I especially liked how you covered the differences between forEach, for...of, and for loops when it comes to skipping iterations. The workaround using return in forEach is a handy reminder, though it definitely makes me appreciate the flexibility of for and for...of more. Thanks for sharing thisโ€”super useful for both beginners and seasoned devs brushing up on fundamentals! ๐Ÿ”๐Ÿ’ก

Collapse
 
nevodavid profile image
Nevo David โ€ข

pretty cool seeing these differences all laid out tbh, you think picking the wrong loop messes up your code long-term or am i just overthinking it

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

DevCycle image

Fast, Flexible Releases with OpenFeature Built-in

Ship faster on the first feature management platform with OpenFeature built-in to all of our open source SDKs.

Start shipping

๐Ÿ‘‹ Kindness is contagious

Explore this practical breakdown on DEVโ€™s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple โ€œthank youโ€ or question in the comments goes a long way in supporting authorsโ€”your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay