Full answer
All three are array methods that take a callback and return a new result instead of changing the array.
map(fn) calls fn for each item and builds a new array from the return values. The new array always has the same length as the old one. Use it when every item has a corresponding output, such as turning marks into grades.
filter(fn) keeps the items for which fn returns a truthy value. The result can be shorter, and the items themselves are not changed.
reduce(fn, initial) carries an accumulator through the array. For each item, fn(accumulator, item) returns the next accumulator, and the final accumulator is the result. It can add numbers, but also group items into an object, count words, or flatten arrays. Always pass the initial value. Without it, the first item is used as the starting accumulator, and an empty array throws a TypeError.
The methods chain well: filter first, then map, then reduce. For very large arrays, a chain makes several passes, so a single reduce or a loop can be better when performance matters.
forEach is different: it returns nothing and is used only for side effects.
const bill = [
{ item: 'Tea', price: 15, qty: 2 },
{ item: 'Samosa', price: 20, qty: 3 },
{ item: 'Water', price: 10, qty: 0 },
];
const lineTotals = bill.map(({ item, price, qty }) => ({ item, total: price * qty }));
console.log(lineTotals);
const ordered = lineTotals.filter((line) => line.total > 0);
console.log(ordered.map((line) => line.item));
const grandTotal = ordered.reduce((sum, line) => sum + line.total, 0);
console.log(grandTotal);
const byItem = bill.reduce((acc, { item, qty }) => {
acc[item] = qty;
return acc;
}, {});
console.log(byItem);
try {
[].reduce((a, b) => a + b);
} catch (error) {
console.log(error.name + ': ' + error.message);
}
console.log(bill.length);Run with Node.js v24.22.1 as js-14.js; two runs gave identical output. Output: [ { item: 'Tea', total: 30 }, { item: 'Samosa', total: 60 }, { item: 'Water', total: 0 } ] [ 'Tea', 'Samosa' ] 90 { Tea: 2, Samosa: 3, Water: 0 } TypeError: Reduce of empty array with no initial value 3
Key points
- map: one output per input, same length, new array.
- filter: keeps items that pass the test, new array.
- reduce: folds to a single value of any type using an accumulator.
- Always give reduce an initial value; an empty array without it throws.
- None of them mutates the original array.
Common mistakes
- Calling reduce on an empty array without an initial value.
- Using map only for side effects and ignoring the returned array; use forEach or for...of.
- Forgetting to return the accumulator in reduce, which makes the next step receive undefined.
- Mutating the accumulator in a way that hides bugs when the initial value is shared between calls.
Follow-ups
- How would you implement map using reduce?
- What is the difference between map, flatMap, and flat?
- Why can chaining many array methods be slow on a huge array, and what could you do instead?
Tips
- Use one data set, such as a canteen bill, to show all three; it keeps the answer short and clear.
- State that none of them mutates, and then say which methods do (push, splice, sort, reverse).
- Show a non-numeric reduce, like grouping, to prove you know it is more than a sum.