Loop (for each) over an array in JavaScript
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
JavaScript offers several ways to loop through arrays, and each one fits a different goal. forEach is convenient for side effects, for...of is flexible with control flow, and methods like map are best for creating transformed arrays. Choosing the right loop improves readability and avoids subtle async and mutation bugs.
forEach for Side-Effect Iteration
forEach runs a callback once for each element in array order.
Use it when you need to perform side effects such as logging, pushing into external arrays, or updating metrics.
Important behavior:
- returns
undefined - cannot break early with
breakorreturnfrom outer function - skips empty slots in sparse arrays
for...of for Flexible Control Flow
When you need break, continue, or early return semantics, for...of is usually better.
This style is especially useful in validations where you stop after first match or failure.
Classic for Loop for Index Control
Traditional for loop remains useful when index arithmetic matters.
Use this when you need neighboring elements, reverse traversal, or custom step sizes.
Use map, filter, and reduce for Data Transformation
If your goal is producing new data, array methods are often cleaner than side-effect loops.
This style communicates transformation intent clearly.
Why for...in Is Usually Wrong for Arrays
for...in iterates enumerable keys, not guaranteed numeric sequence semantics for array iteration use cases.
It can work in simple cases but is better suited to object property iteration. Prefer for...of or forEach for arrays.
Async Iteration Gotcha with forEach
forEach does not await async callbacks.
You will see loop-finished output before async tasks complete.
For sequential async processing, use for...of with await.
For parallel execution, use Promise.all with map.
Mutating Arrays During Iteration
Mutating the iterated array can produce confusing behavior.
When removing items, reverse iteration or immutable filtering is often safer.
Performance and Readability Balance
In most application code, readability matters more than tiny loop performance differences. Choose the construct that best expresses intent.
Practical guideline:
- side effects only:
forEach - early exit control:
for...ofor classicfor - transform into new arrays:
map,filter,reduce - async sequence:
for...ofwithawait
Keep style consistent within module for easier maintenance.
Common Pitfalls
A common pitfall is using forEach with async callbacks and assuming it waits automatically. Another is using for...in on arrays and getting unexpected key behavior. Teams also frequently mutate arrays while iterating without handling index shifts correctly. Finally, overusing one loop style for every task makes code harder to read than necessary.
Summary
- Use loop style based on intent, not habit.
forEachis good for side effects but not for early exit or async waiting.for...ofis flexible and reliable for control flow.map,filter, andreduceare best for transformations.- Avoid
for...infor normal array iteration.
Related reading
- Sort array of objects by string property value
- What and where are the stack and heap?
- A-star algorithm
- A Algorithm for very large graphs, any thoughts on caching shortcuts?
- Loop through an array in JavaScript
- Set cellpadding and cellspacing in CSS?
- A column-vector y was passed when a 1d array was expected
- A difference in style IDictionary vs Dictionary

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.