BeginnerCoder

Lesson 16 — Array reduce

Fold many values into one

reduce(fn, start) walks the array and carries an accumulator — the result so far. For each item it calls fn(acc, item) and the return value becomes the new acc. The second argument is the starting value; for sums use 0.

[10, 25].reduce((acc, p) => acc + p, 0);   // 0 → 10 → 35

You can also find the biggest value: return whichever of acc and p is larger.

Why it matters: totals, maximums and averages are all one reduce away.

Want it another way?
Task

Total the prices with reduce, and find the most expensive price with a second reduce.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with JavaScript Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.