Array Iteration
Array iteration is walking every element to read or transform it. JavaScript offers for, for...of, and methods such as forEach, map, filter, reduce, some, and every.
Detailed Explanation
An array is ordered, so iteration has a defined sequence: index 0 to length - 1 on a dense array. How you walk it changes what you can do and how holes behave.
A numeric for gives you the index and lets you break, continue, and step by more than one. for...of gives you values and also break. for...of on array.entries() gives [index, value]. These are the right tools when the body has early exits or await.
forEach calls a function per present element and cannot break (return is continue). It skips holes. It is synchronous. Do not pass an async callback and expect forEach to wait; it will fire all promises and move on.
map builds a new array of the same length (holes stay holes). filter builds a possibly shorter array of elements that passed a test (holes are skipped). reduce / reduceRight fold values into an accumulator. some and every stop early when the answer is known. find / findIndex / findLast stop at the first match. flat / flatMap merge nested arrays.
for...in is the wrong iterator for arrays. It walks keys as strings, includes extra enumerable properties, and skips holes.
Async iteration over an array is a for...of with await inside, or for await...of on an async iterable. Promise.all(arr.map(fn)) runs work concurrently and waits for all. That is not the same as sequential await in a loop. Pick one: sequential when order or rate limits matter, Promise.all when the work is independent.
What it costs you: accidental extra arrays (map then filter then map allocates three), and incorrect async (forEach + async). A single for that pushes into one pre-sized array is faster for hot paths; method chains are clearer for most app code.
Reach for map/filter when you want a new array. Reach for for...of when you need break or await. Reach for reduce when the result is not an array (a sum, a map, a last-wins object). Reach for some/every/find when you might stop early.
Code Examples
const nums = [1, 2, 3, 4];
console.log(nums.map((n) => n * 2)); // => [ 2, 4, 6, 8 ]
console.log(nums.filter((n) => n % 2 === 0)); // => [ 2, 4 ]
console.log(nums.reduce((sum, n) => sum + n, 0)); // => 10
console.log(nums.some((n) => n > 3)); // => true
console.log(nums.find((n) => n > 2)); // => 3const withHole = [1, , 3];
const seen = [];
withHole.forEach((n) => seen.push(n));
console.log(seen); // => [ 1, 3 ]
console.log([...withHole]); // => [ 1, undefined, 3 ]async function loadAll(ids, fetchOne) {
return Promise.all(ids.map((id) => fetchOne(id)));
}
async function loadOneByOne(ids, fetchOne) {
const out = [];
for (const id of ids) {
out.push(await fetchOne(id));
}
return out;
}
const fake = async (id) => id * 2;
loadAll([1, 2], fake).then((r) => console.log(r)); // => [ 2, 4 ]Common Gotchas
`forEach` does not await
an async callback returns a promise that `forEach` ignores. Use `for...of` or `Promise.all`.
Holes and methods disagree
`forEach`/`map` skip holes; spread and `for...of` see `undefined`. Do not create holes.
`for...in` is not for arrays
you will see extra keys and string indexes.
`map` keeps length, including holes
`[ , 2].map(x => x)` is still holey. `filter(Boolean)` can drop holes and falsy values together.