Map, Filter, Reduce, and Function Composition

Methods such as map, filter, and reduce are higher-order operations over collections. map transforms every element, filter selects elements, and reduce combines values into an accumulator. They are not merely shortcuts; they express different computational intentions.

Composition becomes especially useful when small pure functions are combined into larger transformations.

A Collection Pipeline
const numbers = [1, 2, 3, 4, 5, 6];

const result = numbers
  .filter(n => n % 2 === 0)
  .map(n => n * n)
  .reduce((sum, n) => sum + n, 0);

console.log(result);
💡 The pipeline first selects even values, then squares them, and finally sums the resulting values.
Key Takeaways
  • map transforms elements.
  • filter selects elements.
  • reduce models accumulation into one result.
Knowledge Check

Which method is most directly intended to transform every element into another value?