Glossary / Functional array methods

Functional array methods

Functional array methods process elements through callbacks and often create derived arrays or values without mutating the source. They express transformations declaratively, but the methods differ in output, mutation, short-circuiting, and treatment of sparse holes. Correct use means choosing a method whose contract matches the task, not merely chaining everything.

Core method contracts

const total = orders.reduce((sum, order) => sum + order.amount, 0);

Most callback methods pass (value, index, array) and generally skip absent slots in sparse arrays. Passing functions with incompatible signatures can cause bugs (map(parseInt) passes index as radix). Avoid map if the mapped array is discarded; use forEach or a loop for effects. Do not use reduce for every problem if it hides intent.

Methods such as sort, reverse, splice, and fill mutate; map, filter, and slice return a new outer array but do not deep-copy elements. New copying methods such as toSorted have target support requirements.

Interview questions

  1. When use map rather than forEach? When each visited element should produce a transformed result array.
  2. Which methods short-circuit? find, some, and every among common methods.
  3. Why give reduce an initial value? It defines accumulator type and handles empty arrays.
  4. Does filter deep-copy its elements? No, it returns references to selected values.

References