To iterate over an array in JavaScript, use for...of when you need each value, forEach() for a simple side effect on every element, and map(), filter() or reduce() when you want to build a new result. Use a classic for loop when you need the index or maximum control, and avoid for...in for arrays.
All of these methods are available in current browsers and Node.js. The best choice depends on whether you need to stop early, await asynchronous work, or produce a new value.
| Method | Returns | Can break early | Works with await in order |
|---|---|---|---|
for | Nothing | Yes | Yes |
for...of | Nothing | Yes | Yes |
forEach() | undefined | No (throw to stop) | No |
map() | New array | No | No |
filter() | New array | No | No |
reduce() | Single value | No | No |
some() / every() | Boolean | Stops by itself | No |
for...in | Nothing | Yes | Yes (but wrong tool) |
for...of is the cleanest way to read every value of an array. It works with any iterable, supports break, continue and return, and works with await.
When you need the index too, combine it with entries().
A for loop with an index is useful when you need to step by more than one, iterate backwards, or modify the loop variable.
It is also the fastest option in very hot code paths, although for normal application code the difference is rarely noticeable. Prefer readability first.
forEach() calls a function once for each element, with the value, index and the array itself as arguments.
Two limitations catch people. You cannot stop a forEach with break or return from the outer function, and it ignores the promise returned by an async callback, so it does not wait.
These methods return new values and do not change the original array, which makes them good for data transformation.
Use some() to test whether any element matches, every() to test whether all match, and find() or findLast() to get the first or last matching element. These stop as soon as the answer is known.
To run asynchronous work one item at a time, use for...of with await. To run everything in parallel, use Promise.all with map.
Top-level await works in ES modules. Inside functions, mark them async.
for...in loops over enumerable property names, not values. For arrays it yields string indexes, and it also visits any extra properties added to the array or its prototype chain.
Use for...of or forEach instead. Reserve for...in for plain objects, and consider Object.keys(), Object.values() or Object.entries() there.
For most code, for...of is the best default because it is readable and supports break, continue and await. Choose map, filter or reduce when you are producing a new value.
Use for (const [i, v] of arr.entries()), or the second parameter of forEach, map and filter callbacks.
No. Use for...of, a classic for loop, or some() and every(), which stop early when the result is decided.
Adding or removing elements during iteration can skip or repeat items. Iterate over a copy such as [...arr], or build a new array with filter() or map().
Use for...of as the default, forEach for simple side effects, and map, filter and reduce for transformations. Pick a classic for loop when you need index control, use for...of with await for sequential async work, and avoid for...in on arrays.