Module 6 · Lesson 2 of 422/32 in the course~18 min
Module lessons (2/4)
reduce
Lesson preview5 hands-on exercises readyJavaScript Course · Module 6 — Functional array methods
Aggregate an array into a single value: sums, counts, maps.
What you'll learn in this lesson
- Master
.reduce((acc, elem) => nextAcc, initialValue)to synthesize an array into a single value (number, object, or map). - Understand why always specifying
initialValueavoidsTypeErroron empty arrays and establishes the return type. - Construct counts and groupings (
groupBy) by accumulating in an object orMap. - Choose between a single
.reduce()and a chain.filter().map()based on clarity.
Overview
.reduce() is the universal aggregation construct for JavaScript arrays: it keeps an accumulator updated element by element, and upon completion returns that accumulator as the final result — which can be a number, a string, or a grouping object.
Sections covered in this lesson
- 1Mental model: the state flowing in the accumulator
- 2Progressive examples
- 3Guided laboratory
- 4Check for understanding
Pro Lesson · Module 6 — Functional array methods
Unlock "reduce" and the full JavaScript Course curriculum
Aggregate an array into a single value: sums, counts, maps.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in JavaScript Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.