Every JavaScript developer works with arrays daily — API responses, form data, shopping carts, user lists. You can process all of it with for loops, but modern JavaScript gives you methods that are shorter, clearer, and harder to get wrong. These are the ones worth mastering.
map — Transform Every Item
map takes an array and returns a new array of the same length, with each item transformed:
const prices = [100, 250, 400];
const withTax = prices.map(price => price * 1.1);
// [110, 275, 440]
Use map whenever you convert data into a new shape — for example, turning API users into dropdown options. It never changes the original array.
filter — Keep Only What Matches
filter returns a new array containing only the items that pass your test:
const products = [
{ name: 'Milk 1L', inStock: true },
{ name: 'Eggs 12', inStock: false },
{ name: 'Bread', inStock: true },
];
const available = products.filter(p => p.inStock);
// [{ name: 'Milk 1L', ... }, { name: 'Bread', ... }]
Search boxes, status filters and permission checks are all filter in disguise.
reduce — Boil an Array Down to One Value
reduce is the most powerful — and the most confusing — of the three. It walks the array and accumulates a single result:
const cart = [
{ item: 'Milk', price: 280, qty: 2 },
{ item: 'Bread', price: 120, qty: 1 },
];
const total = cart.reduce((sum, line) => sum + line.price * line.qty, 0);
// 680
The 0 at the end is the starting value — forgetting it is the most common reduce bug. Beyond totals, reduce can count items, group data, or build objects. If a simple loop reads more clearly, though, use the loop. Readability wins.
find and findIndex — Get One Item
find returns the first item that matches, or undefined:
const user = users.find(u => u.id === 7);
Use findIndex when you need the position instead — for example, before updating or removing an item.
some and every — Answer Yes/No Questions
const hasOutOfStock = products.some(p => !p.inStock); // any?
const allInStock = products.every(p => p.inStock); // all?
These stop early as soon as the answer is decided, which makes them efficient for validation: "does any field fail?", "are all steps complete?"
flatMap — Map and Flatten in One Step
flatMap maps each item to an array, then flattens the result one level:
const orders = [
{ id: 1, items: ['milk', 'bread'] },
{ id: 2, items: ['eggs'] },
];
const allItems = orders.flatMap(o => o.items);
// ['milk', 'bread', 'eggs']
Perfect for tags, categories, and line items nested inside orders.
Chaining — Where It All Comes Together
The real power appears when you combine methods. Total revenue from completed orders only:
const revenue = orders
.filter(o => o.status === 'completed')
.map(o => o.total)
.reduce((sum, n) => sum + n, 0);
Each step reads like a sentence. This is exactly the shape of data you get back from an API — the Laravel REST API tutorial shows the backend side of that story.
Quick Reference
| Method | Returns | Use it when |
|---|---|---|
map |
New array, same length | Transforming every item |
filter |
New array, shorter or equal | Keeping items that match |
reduce |
Single value | Totals, counts, grouping |
find |
One item or undefined |
Looking something up |
some / every |
true / false |
Validation questions |
flatMap |
New flattened array | Unnesting one level |
Master these six and most loop-heavy code in your projects becomes two or three readable lines. More guides like this are in the JavaScript category.