Course contentsShow
← JavaScript
Lesson 453 of 2,72312. Higher-Order FunctionsFree lesson

What Reduce Does

Understanding reduce as a pattern for accumulating a single result from an array by applying a function repeatedly.

What Reduce Does

You'll learn: How reduce transforms an array into a single accumulated value by repeatedly applying a function.

The Core Idea

Think of reduce like a snowball rolling down a hill—it starts with an initial value and accumulates more with each element it encounters. Unlike map (which transforms every element) or filter (which selects elements), reduce collapses an entire array down to one result.

That result could be anything: a number, a string, an object, or even another array. The key is that you're accumulating as you go.

How It Works (Conceptually)

Imagine you have an array of numbers and want their total. With a regular loop, you'd:

  1. Start with zero
  2. Add the first number
  3. Add the second number to that total
  4. Keep going until done

reduce does exactly this, but in a functional style. You provide:

  • A callback function that describes how to combine the accumulator with each element
  • An initial value for the accumulator to start from

The function runs once per array element, each time updating the accumulator, then finally returns the accumulated result.

Common Use Cases

  • Summing numbers
  • Finding maximum or minimum values
  • Counting occurrences
  • Flattening nested arrays
  • Building objects from arrays
  • Combining strings

Key Takeaway: reduce is the pattern for boiling down an array into a single accumulated result by repeatedly applying a combining function—it's your go-to tool when you need to transform many values into one.