All 28 lessons ▾

Inside JavaScript · Lesson 19 of 28

map, filter and reduce

Updated 11 Nov 2026 · By Akshay, software engineer, India · 9 min read

So, map, filter, and reduce. Three functions you'll use every single day, and every interviewer asks about. So let's master all three, with real examples.

The video for this lesson is coming soon.

A new lesson goes up every day at 11 am. Subscribe on YouTube so you don't miss it.

Got a doubt? Ask in the comments under the video.Subscribe

In one minute
  • map: transform every item, same length
  • filter: keep the items that return true
  • reduce: combine everything into one value
  • acc = the result so far, curr = this item
  • Always give reduce a starting value
  • Chain them: filter, then map

Part 1map: transform every item

So in the last episode we built calculate, and found out it was map. So today, we master map, and its two best friends, filter and reduce. All three are higher-order functions. They take your function, and do the looping for you. So let's start with map.

REMEMBER THIS
maptransformseveryitem,and
returnsanewarray.
same length as the original, which stays untouched

So map is for transformation. It calls your function once for every item in the array, and puts whatever you return into a brand new array. So if you have five items, you always get five items back, each one transformed. And the original array? Untouched. Now remember this. Same length, new array, every item transformed.

map.js
1const prices = [120, 80, 250];2 3const withFee = prices.map(function (p) {4  return p + 20;5});6// [140, 100, 270]7 8const labels = prices.map((p) => "₹" + p);9// ["₹120", "₹80", "₹250"]

So here's a food order with three prices. One twenty, eighty, and two fifty. The app adds a delivery fee of twenty to each. So we map, with a function that returns p plus twenty. We get one forty, one hundred, and two seventy. Now a second transformation. Showing prices on screen, with the rupee sign. Map again, with an arrow function this time, and we get labels like rupee one twenty. Two different transformations, same simple pattern. So you write the logic for one item, and map does it for all of them.

BONUS
forEachreturnsnothing.
mapreturnsanewarray.
map to transform; forEach just to do something

And now a quick bonus, because interviewers ask this a lot. What's the difference between forEach and map? Both call your function for every item. But forEach returns nothing. It gives back undefined. It's only for doing something with each item, like logging, or saving. Now map gives you a new array of results. So, need a new array? Use map. Just doing something with each item? Use forEach.

Part 2filter: keep only what you want

Next, filter. Now if map is about changing items, filter is about choosing them.

REMEMBER THIS
filterkeepsonlytheitemswhereyour
functionreturnstrue.
the new array can be shorter

So filter calls your function for every item, and your function answers one question. Keep it, or not? If it returns true, the item goes into the new array. If it returns false, it's left out. So the result can be shorter than the original. Now remember this. map changes items, filter chooses items.

filter.js
1const prices = [120, 80, 250, 40, 300];2 3const cheap = prices.filter(function (p) {4  return p < 100;5});6// [80, 40]7 8const premium = prices.filter((p) => p >= 250);9// [250, 300]

So now our menu has five prices. A customer wants something cheap, under a hundred. So we filter, with a function that returns p less than one hundred. For one twenty, that's false, so it's dropped. Eighty, true, kept. Two fifty, dropped. Forty, kept. Three hundred, dropped. Result, eighty and forty. And for the premium section, items of two fifty or more, one more filter, and we get two fifty and three hundred. So you just write the condition. filter does the rest. Simple.

Part 3reduce: boil it down to one

And now, the one that confuses most people. reduce. But don't worry. Once you see it step by step, you'll never forget it.

REMEMBER THIS
reduceboilsthewholearraydownto
onevalue.
acc carries the result so far; curr is the current item

So reduce takes all the items in an array, and reduces them to a single value. A total, a maximum, an object, anything. It works with two things. The accumulator, called acc, which carries the result so far. And curr, the current item. Your function gets both, and returns the new acc. And you also give reduce a starting value for acc.

loop-sum.js
1const prices = [120, 80, 250];2 3let sum = 0;4for (let i = 0; i < prices.length; i++) {5  sum = sum + prices[i];6}7// sum is 450

So first, the old way, to compare. To add up the bill, we start sum at zero, loop through the prices, and keep adding each one to sum. At the end, sum is four fifty. Now keep two things in mind. sum, the running total. And prices of i, the current item. So reduce has exactly these two, just with different names.

reduce.js
1const prices = [120, 80, 250];2 3const total = prices.reduce(function (acc, curr) {4  return acc + curr;5}, 0);6 7console.log(total);
Output
450

So now let's watch reduce do the same thing. prices is set. Line three calls reduce, and reduce calls our function for the first item. acc starts at zero, the starting value we passed at the end. And curr is the first price, one twenty. The function returns zero plus one twenty. So now, acc is one twenty. Second call. acc is one twenty, and curr is eighty. That gives two hundred. Third call. acc is two hundred, and curr is two fifty. It returns four fifty. No more items, so reduce hands back the final acc. total is four fifty, and line seven prints it.

REMEMBER THIS
accistherunningresult,curristhe
item,andthelastargumentisacc's
start.
acc = sum, curr = prices[i]

So remember this mapping. acc is the sum. curr is prices of i. And the zero at the end is where acc starts, just like let sum equals zero. Now if you forget the starting value, reduce uses the first item as acc instead, and on an empty array, it throws an error. So always give it a start.

max.js
1const prices = [120, 80, 250, 40, 300];2 3const max = prices.reduce(function (max, curr) {4  return curr > max ? curr : max;5}, 0);6// 300

And now reduce isn't just for sums. Here, we find the most expensive item. acc is now called max, because acc is just a label. You can name it anything. For each price, if curr is bigger than max, it becomes the new max. Otherwise, max stays. So start at zero, and after five items, max is three hundred.

Part 4Real-world tricks

Now let's use all three on real data, the kind you'll see in every project, and in interviews.

users.js
1const users = [2  { first: "Asha", last: "Rao", age: 23 },3  { first: "Kabir", last: "Mehta", age: 29 },4  { first: "Meera", last: "Iyer", age: 29 },5  { first: "Rohan", last: "Das", age: 50 },6];7 8const fullNames = users.map((u) => u.first + " " + u.last);9// ["Asha Rao", "Kabir Mehta", "Meera Iyer", "Rohan Das"]

So here's a list of users, each an object with a first name, a last name, and an age. Task one. Get everyone's full name. Now which function should we use? We want one result per user, so it's map. For each user, return first, a space, and last. Asha Rao, Kabir Mehta, and so on. Easy.

report.js
1const report = users.reduce(function (acc, u) {2  acc[u.age] = (acc[u.age] || 0) + 1;3  return acc;4}, {});5// { 23: 1, 29: 2, 50: 1 }

Now task two, a classic interview question. How many users are there of each age? We want one single object out of the whole array, so it's reduce, starting with an empty object. For each user, look at acc of their age. If it's not there yet, start from zero, then add one. Asha, twenty three, gets one. Kabir, twenty nine, gets one. Meera is also twenty nine, so that becomes two. Rohan, fifty, gets one. So the final report. Twenty three, one. Twenty nine, two. Fifty, one.

chain.js
1const young = users2  .filter((u) => u.age < 30)3  .map((u) => u.first);4// ["Asha", "Kabir", "Meera"]

Now task three. First names of everyone under thirty. This needs two steps. First, keep only the users under thirty. That's filter. Then, take just their first names. That's map. And since filter returns an array, we can call map right on it. This is called chaining. Asha, Kabir, and Meera. So read it out loud, and it almost sounds like English. Filter the young ones, then map their names.

chain-reduce.js
1const young = users.reduce(function (acc, u) {2  if (u.age < 30) {3    acc.push(u.first);4  }5  return acc;6}, []);7// ["Asha", "Kabir", "Meera"]

And now here's a challenge interviewers love. Can you do the same with only reduce? Yes! Start acc as an empty array. For each user, if they're under thirty, push their first name into acc. Return acc every time. So one pass through the array, instead of two. Both answers are correct, but this one shows you really understand reduce.

INTERVIEW ANSWER
maptransforms,filter
selects,reducecombines
intoonevalue.
all three return new values; none changes the original
Quick quiz
quiz.js
1const nums = [1, 2, 3, 4];2 3const result = nums4  .map((n) => n * 10)5  .filter((n) => n > 15)6  .reduce((acc, n) => acc + n, 0);

What's result?

Show the answer

So first, map multiplies every number by ten. Ten, twenty, thirty, forty. Then filter keeps those bigger than fifteen. Twenty, thirty, forty. Then reduce adds them up, starting from zero. Ninety! Did you get ninety? So that's map, filter, and reduce, all chained together.

Your turn

Which one do you find the hardest?

Answer in the comments under the video.

Your progress stays on this device.
A new lesson every day at 11 am

The video on YouTube, and the lesson as a swipe-through on Instagram.