Pure Functions and Array Methods
You'll learn how JavaScript's built-in array methods like map, filter, and reduce are designed to work perfectly with pure functions, enabling clean transformations without side effects.
Why Array Methods and Purity Go Hand-in-Hand
JavaScript's functional array methods (map, filter, reduce) encourage a pure approach: they don't modify the original array and they expect you to provide a pure transformation function. This combination lets you transform collections predictably.
Think of it like an assembly line: each station (method) receives items, transforms them according to instructions (your pure function), and passes along new results—without altering what came before.
The Core Pattern
map() — Transforms each element, returning a new array of the same length:
const prices = [10, 20, 30];
const doubled = prices.map(price => price * 2);
// [20, 40, 60] — original prices unchanged
filter() — Selects elements that pass a test, returning a new array:
const ages = [12, 18, 25, 16];
const adults = ages.filter(age => age >= 18);
// [18, 25] — original ages unchanged
reduce() — Accumulates elements into a single value:
const nums = [1, 2, 3, 4];
const sum = nums.reduce((total, n) => total + n, 0);
// 10 — original nums unchanged
Keeping It Pure
Each callback you pass should be pure: it receives values, returns a result, and doesn't mutate or access external state. This makes your data transformations predictable, testable, and easy to reason about.
Key Takeaway: map, filter, and reduce work best with pure functions—they create new values without modifying originals, giving you safe, composable transformations of collections.