Array Methods: Filter, Map, Reduce in JavaScript
Learn JavaScript filter, map and reduce with simple examples and chaining. They build new arrays and keep the original, with explanation and output.
1let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];23// FILTER: Creates new array with elements that pass test4let evenNumbers = numbers.filter(num => num % 2 === 0);5console.log("Original:", numbers);6console.log("Even numbers (filter):", evenNumbers);78let greaterThan5 = numbers.filter(num => num > 5);9console.log("Numbers > 5:", greaterThan5);1011// MAP: Creates new array by transforming each element12let doubled = numbers.map(num => num * 2);13console.log("\nDoubled (map):", doubled);1415let squared = numbers.map(num => num ** 2);16console.log("Squared:", squared);1718// REDUCE: Reduces array to single value19let sum = numbers.reduce((acc, num) => acc + num, 0);20console.log("\nSum (reduce):", sum);2122let product = numbers.reduce((acc, num) => acc * num, 1);23console.log("Product:", product);2425// CHAINING: Combine multiple methods26let result = numbers27 .filter(num => num % 2 === 0) // [2, 4, 6, 8, 10]28 .map(num => num * 2) // [4, 8, 12, 16, 20]29 .reduce((acc, num) => acc + num, 0); // 603031console.log("\nChained (even * 2, then sum):", result);3233// Real-world example: Processing user data34let users = [35 { name: "John", age: 25, active: true },36 { name: "Jane", age: 30, active: false },37 { name: "Bob", age: 20, active: true },38 { name: "Alice", age: 35, active: true }39];4041// Get names of active users over 2542let activeOver25 = users43 .filter(user => user.active && user.age > 25)44 .map(user => user.name);4546console.log("\nActive users over 25:", activeOver25);4748// Calculate average age of active users49let avgAge = users50 .filter(user => user.active)51 .map(user => user.age)52 .reduce((acc, age, index, array) => {53 acc += age;54 if (index === array.length - 1) {55 return acc / array.length;56 }57 return acc;58 }, 0);5960console.log("Average age of active users:", avgAge);
Sample Output
Active users over 25: [ 'Alice' ]
Jane is over 25 but not active, and Bob is active but 20. Only Alice passes both checks.
Average age of active users: 26.666666666666668
(25 + 20 + 35) / 3. The long tail is normal decimal rounding.
In this one
- What filter, map and reduce each return
- How to chain them into one pipeline
- How to use them on arrays of objects
- Why they do not change the original array
How it works
- 1
filter keeps even numbers and numbers above 5.
- 2
map doubles and squares every number.
- 3
reduce gives sum 55 and product 3628800.
- 4
The chain gives 60.
- 5
User filtering returns [ "Alice" ].
- 6
The average age of active users is 26.67 (rounded).
The chain on [1..10]
What each step hands to the next.
- 1.filter → [2, 4, 6, 8, 10]
- 2.map → [4, 8, 12, 16, 20]
- 3.reduce → 4 + 8 + 12 + 16 + 20 = 60
Edge cases
Forgetting return in a block arrow
map(n => { n * 2 }) gives [undefined, ...].
With curly braces you must write return. Without braces the value is returned automatically.
reduce without a start value on an empty array
[].reduce((a, b) => a + b) throws.
Pass 0 or another start value.
Long decimals
The average prints 26.666666666666668.
Use toFixed(2) when showing it to users.
What's going on
filter, map and reduce are three array methods you will use every day. Each takes a callback, which is a function you pass in to run on every item. None of them change the original array. They return new values.
filter keeps items that pass a test. The callback returns true or false. numbers.filter(num => num % 2 === 0) keeps only even numbers. The result can be shorter than the original.
map changes each item. The result always has the same length. numbers.map(num => num * 2) doubles every value. Use map when you want a new value for every item.
reduce combines all items into one result. It keeps an accumulator, a running value, starting from the value you pass at the end. With + and 0 it gives a sum. With * and 1 it gives a product.
You can chain them. Here, filter keeps 2, 4, 6, 8, 10. map doubles them to 4, 8, 12, 16, 20. reduce adds them to 60. Each step reads like a sentence.
The user example works on objects. "Active users over 25" filters on two conditions and maps to names. Only Alice is both active and older than 25. Jane is 30 but inactive.
The average-age part works, but it is tricky: reduce divides on the last item. A clearer way is to sum with reduce, then divide by the length. The active ages are 25, 20 and 35, so the average is 80 / 3 = 26.67.
Prefer these chains over forEach pushing into an outside array. That pattern is where bugs hide.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does [1, 2, 3].map(n => n * 10) return?
Pick an answer to check it.
Question 2
Which method returns a single value?
Pick an answer to check it.
Question 3
Does filter change the original array?
Pick an answer to check it.
Questions people ask
Which is faster, a for loop or a chain?
A plain loop does less work, but chains are clearer. For normal data the difference does not matter.
Does map ever change the length?
No. Use filter to remove items.
Why is Jane not in the result?
Her active value is false, so the first filter condition fails.