Difficulty: Beginner
Explain the key array methods in JavaScript. How do map, filter and reduce differ, and when would you use each?
map, filter and reduce are higher-order array methods that take a callback and return a new value without mutating the original array. map transforms every element and returns an array of the same length, filter keeps the elements whose callback returns truthy, and reduce folds the whole array into a single value of any shape, such as a sum, an object or a grouped map.
| Method | Callback returns | Result | Length |
|---|---|---|---|
map | The new element | New array | Same as input |
filter | Truthy or falsy | New array | Same or fewer |
reduce | The next accumulator | Any value | n/a |
forEach | Ignored | undefined | n/a |
find / findIndex | Truthy or falsy | First match or undefined / -1 | n/a |
some / every | Truthy or falsy | Boolean, short-circuits | n/a |
The callback receives (element, index, array). reduce receives (accumulator, element, index, array) and takes an optional initial value as its second argument.
reduce starts with the initial value as the accumulator, calls the callback for each element, and uses whatever the callback returns as the accumulator for the next call. The final accumulator is the result. Always pass an initial value: without one, reduce uses the first element as the start, and on an empty array it throws TypeError: Reduce of empty array with no initial value.
Use reduce when the output shape differs from the input, for example totalling a cart in paise or grouping orders by status. For grouping, Object.groupBy(items, fn) (ES2024, available in Node 21+ and all current browsers) is now a clearer alternative.
Because map and filter return arrays, you can chain them: orders.filter(isPaid).map(toInvoice). Each step creates an intermediate array and loops again. For a few thousand items this does not matter and readability wins. For hot paths over large data, a single reduce or a plain for...of loop avoids the extra allocations. Iterator helpers (Iterator.prototype.map, .filter, .take) shipped in ES2025 and are available in current Chrome and Node 22+, giving lazy chaining without intermediate arrays.
map for side effects and ignoring the result. Use forEach or for...of instead.return inside a block-body arrow: arr.map(x => { x * 2 }) gives [undefined, ...].['1', '2', '3'].map(parseInt) returns [1, NaN, NaN] because map passes the index as parseInt's radix argument.forEach to stop early. It cannot be broken out of; use for...of with break, or some/find.sort, reverse and splice change the original array. ES2023 added toSorted, toReversed, toSpliced and with as non-mutating versions, which matter in React state updates.const orders = [
{ id: 1, status: 'PAID', amountPaise: 24900 },
{ id: 2, status: 'FAILED', amountPaise: 9900 },
{ id: 3, status: 'PAID', amountPaise: 15000 },
{ id: 4, status: 'PAID', amountPaise: 5000 },
];
const paidTotal = orders
.filter((o) => o.status === 'PAID')
.map((o) => o.amountPaise)
.reduce((sum, amt) => sum + amt, 0);
console.log(paidTotal / 100); // rupees
console.log(orders.length); // original unchanged
filter narrows, map reshapes, reduce aggregates. The initial value 0 keeps reduce safe on an empty array.
const orders = [
{ id: 1, city: 'Pune' }, { id: 2, city: 'Delhi' }, { id: 3, city: 'Pune' },
];
const countByCity = orders.reduce((acc, o) => {
acc[o.city] = (acc[o.city] ?? 0) + 1;
return acc; // always return the accumulator
}, {});
console.log(countByCity);
// ES2024: group without writing reduce by hand
const grouped = Object.groupBy(orders, (o) => o.city);
console.log(Object.keys(grouped));
reduce can build any shape. Object.groupBy covers the common grouping case directly in modern runtimes.
console.log(['1', '2', '3'].map(parseInt)); // [ 1, NaN, NaN ]
console.log(['1', '2', '3'].map(Number)); // [ 1, 2, 3 ]
console.log([1, 2].map((x) => { x * 2; })); // [ undefined, undefined ]
const scores = [40, 10, 30];
const sorted = scores.toSorted((a, b) => a - b); // ES2023, non-mutating
console.log(sorted, scores);
map passes (value, index), which breaks parseInt. A block-body arrow needs an explicit return. toSorted sorts without touching the original.
map, filter, reduce, Immutability, Method Chaining, Array.prototype