map, filter, and reduce are the three array methods that replace most loops in modern JavaScript. They are short, composable, and — once they click — you will reach for them before writing a for loop.
map transforms every item and returns a new array. It never changes the original:
const prices = [100, 250, 400];
const withTax = prices.map((price) => price * 1.18);
// [118, 295, 472]
filter keeps only the items that pass a test:
const users = [
{ name: "Asha", active: true },
{ name: "Ravi", active: false },
{ name: "Meera", active: true },
];
const activeUsers = users.filter((user) => user.active);
// [{ name: "Asha", ... }, { name: "Meera", ... }]
reduce boils an array down to one value — a sum, an object, a string:
const cart = [
{ item: "Book", price: 199 },
{ item: "Pen", price: 49 },
{ item: "Notebook", price: 99 },
];
const total = cart.reduce((sum, product) => sum + product.price, 0);
// 347
The second argument to reduce is the starting value — 0 for a sum, {} for building an object, [] for building an array. Forgetting it is the most common reduce bug: without it, the first item becomes the starting value and the iteration starts from the second.
The real power is chaining. Filter, then map, then reduce — a data pipeline in one expression:
const orders = [
{ item: "Book", price: 199, delivered: true },
{ item: "Pen", price: 49, delivered: false },
{ item: "Course", price: 999, delivered: true },
];
const deliveredTotal = orders
.filter((order) => order.delivered)
.map((order) => order.price)
.reduce((sum, price) => sum + price, 0);
// 1198
Each step returns a new array, so the chain reads top to bottom like a recipe. Compare that with a nested for loop full of if statements and temporary variables.
Three methods worth learning next: find returns the first match instead of an array, some and every answer "does any item match?" and "do all items match?", and sort orders an array in place — remember it mutates the original, so copy first with [...array].sort().
Two mistakes beginners make: first, using map when they want a side effect — map is for building a new array; use forEach when you just want to do something with each item. Second, chaining without checking the intermediate result — log each step while learning, and the pipeline stops being mysterious.
Ways to extend it: rewrite an old for loop with these three methods, group an array by a key with reduce, or build a small data dashboard that filters, transforms, and totals a list in one chain. These three methods show up in nearly every JavaScript job interview for a reason — they are the vocabulary of working with data.
Top comments (0)