BeginnerCoder

Lesson 13 — reduce

Fold a list into one value

reduce walks through an array and carries an accumulator — a running result — from item to item. You give it a function (accumulator, item) => newAccumulator and a starting value. The result can be a number, a string, or even an object that counts things.

const total = [5, 10, 20].reduce((sum, n) => sum + n, 0);
console.log(total); // 35

Why it matters: totals, averages, grouping and counting are everyday tasks, and reduce does all of them.

Want it another way?
Task

Finish the reducer so it counts how many times each fruit appears: apple 3, pear 2, kiwi 1.

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.