Coding/Learn/javascript/array methods iterators
JavaScript Foundational Course
01.Introduction to JavaScript & V8 Engine
02.Console Output & Program Flow
03.Variables & Data Types (let, const, typeof)
04.Template Literals & String Interpolation
05.Type Coercion & Strict Equality (=== vs ==)
06.Arithmetic, Math Object & Modulo
07.Conditionals & If / Else Branching
08.Logical Operators & Nullish Coalescing (??)
09.Ternary Operator & Switch Statements
10.While & Do-While Loops
11.Standard For Loops & Iteration
12.Loop Control (Break & Continue)
13.Functions, Arrow Syntax & Return Values
14.Default Parameters, Rest & Spread Syntax
15.Arrays, Indexing & Mutating Methods
16.Array Iterators (map, filter, reduce)
17.Objects, Key-Value Pairs & Destructuring
18.Scope, Lexical Environment & Closures
19.Asynchronous JavaScript (Promises & Async/Await)
20.Capstone: Interactive E-Commerce Cart & Ledger Engine
Course Certificate0%

Complete all lessons to earn

LearnArray Iterators (map, filter, reduce)
16 / 20 0%
Lesson 16 of 20
6 mins read

Array Iterators (map, filter, reduce)

PreviousNext Lesson
In Plain English

Modern JavaScript leverages functional iterator methods (.map, .filter, .reduce, .find, .some, .every) to process collections declaratively without manual for loop counter bookkeeping.

The Mental Model

"Imagine a factory assembly line: .filter() discards defective items, .map() paints the remaining items, and .reduce() packages all painted items into one shipping container."

Deep Dive: How It Works

.map(fn): Creates a brand new array by applying fn to every element.

.filter(fn): Creates a new array containing only elements where fn returned truthy.

.reduce((acc, curr) => ..., init): Condenses an array down into a single accumulated result (sum, object, histogram).

Syntax Blueprint

items.filter(x => x.active).map(x => x.price).reduce((a, b) => a + b, 0)

Chain array methods cleanly for expressive, readable data transformation pipelines.

PointCore Rules to Remember

Point star
.map(): Transforms every item 1-to-1 into a new array.
Point star
.filter(): Selects items matching a predicate test.
Point star
.reduce(): Accumulates array values into a single summary total or structure.

Common Beginner Traps & How to Fix Them

Mistake:Forgetting the initial value in .reduce().

Why it happens: Without an initial value, reduce takes the first element as the accumulator, which can cause type bugs on empty arrays or object transformations.

How to fix: Always provide an explicit initial value: .reduce((sum, n) => sum + n, 0).

Pro Tip: .map and .filter do not mutate the original array—they return fresh transformed copies.

Live Interactive Example

Hit Run Code to see it live
Functional Data Pipeline
JavaScript ES2024
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: Shows filter, map, and reduce executing in idiomatic modern JavaScript.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Filter and sum numbers with reduce

  • PointGiven const scores = [10, 20, 30, 40];
  • PointUse scores.filter(s => s >= 20) to filter scores >= 20.
  • PointUse .reduce((a, b) => a + b, 0) on the filtered list to find the sum (90).
  • PointPrint "Qualified Total: 90".
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates