Facebook Pixel
Chaining map, filter, and reduce in JavaScript

Why is reduce usually last in a chain in JavaScript?

The answer

Because reduce returns a single value (not an array), so you cannot chain array methods after it. map and filter return arrays, so they can be chained. reduce is the final step that combines everything.

More FAQs in Chaining map, filter, and reduce in JavaScript

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).

Each chain step creates a new array (multiple passes). For large arrays, a single reduce or loop may be faster. But chaining is more readable. Optimize only if profiling shows a problem.

products.filter(p => p.active).map(p => p.price).reduce((acc, price) => acc + price, 0). Filter active products, extract prices, sum them.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.