Back
LearnJavaScript From Zerofilter, find & reduce — search and combine

filter, find & reduce — search and combine

2 min read

Three more tools you will use every day

Once you have map, three more methods complete your toolkit: filter, find, and reduce. Each takes a function and answers a different question.

filter — keep only what matches

filter keeps the items where your condition is true, and gives back a new array.

let ages = [12, 18, 25, 9, 40];
let adults = ages.filter((age) => age >= 18);
console.log(adults);   // [18, 25, 40]

Read it as: "keep every age that is 18 or more".

find — get the first match

find is like filter, but returns only the first matching item — not an array.

let users = [
  { name: "Riya", age: 25 },
  { name: "Sam", age: 30 }
];

let sam = users.find((u) => u.name === "Sam");
console.log(sam.age);   // 30

reduce — boil a list down to one value

reduce combines all items into a single result — a total, a maximum, anything.

let cart = [100, 250, 50];
let total = cart.reduce((sum, item) => sum + item, 0);
console.log(total);   // 400

Here sum starts at 0 (the last argument) and adds each item, one by one, until you get one final number.

Which one, when?

  • Want a smaller list? → filter
  • Want one specific item? → find
  • Want one combined value (total, average)? → reduce

map, filter, find, reduce — these four cover almost everything you will ever do with data. Master them and you are writing real, clean JavaScript.

Tip