Facebook Pixel

map, filter, and reduce in JavaScript: A Complete Guide

The three most important array methods. Here is how each works, when to use them, and how to chain them.

map, filter, and reduce in JavaScript: A Complete Guide

map, filter, and reduce are the three most important array methods in JavaScript. They are higher-order functions that enable functional programming.

map: Transform Each Element

const doubled = [1, 2, 3].map((n) => n * 2); // [2, 4, 6] `` - Calls the callback on each element. - Returns a new array with the results. - Same length as the original. - Does not mutate the original. ### filter: Select Elements ```js const evens = [1, 2, 3, 4].filter((n) => n % 2 === 0); // [2, 4] `` - Calls the callback on each element. - Includes elements where the callback returns truthy. - New array may be shorter than the original. - Does not mutate the original. ### reduce: Combine Into One Value ```js const sum = [1, 2, 3].reduce((acc, n) => acc + n, 0); // 6 `` - Calls the callback with an accumulator and each element. - The return value becomes the next accumulator. - The second argument (`0`) is the initial value. - Returns the final accumulator. - Can produce any type (number, string, array, object). ### Chaining ```js const result = [1, 2, 3, 4] .filter((n) => n > 1) // [2, 3, 4] .map((n) => n * 2) // [4, 6, 8] .reduce((acc, n) => acc + n, 0); // 18 `` `map` and `filter` return arrays, so they can be chained. `reduce` returns a single value, so it is usually last. ### Callback Arguments All three callbacks receive `(element, index, array)` (reduce receives `(accumulator, element, index, array)`): ```js arr.map((el, i, arr) => ...); arr.filter((el, i, arr) => ...); arr.reduce((acc, el, i, arr) => ..., init); `` ### The Takeaway `map` transforms (same length), `filter` selects (subset), `reduce` combines (single value). They are higher-order functions that take callbacks, return new values, and do not mutate the original. They can be chained for powerful data transformations.

map transforms each element and returns a new array of the same length. filter selects elements that pass a test and returns a shorter (or equal) array. reduce combines all elements into a single value using an accumulator.

No. All three return new values (a new array for map and filter, a single value for reduce). The original array is unchanged.

map and filter callbacks receive (element, index, array). reduce callback receives (accumulator, element, index, array). Most callbacks only use the first one or two, but all are available.

Yes. map and filter return new arrays, so you can chain them. reduce returns a single value, so it is usually last. Example: arr.filter(n => n > 1).map(n => n * 2).reduce((a, b) => a + b, 0).

To avoid bugs with empty arrays (TypeError) and type mismatches (when the accumulator type differs from the element type, like building an object from an array of objects).

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.