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
map: one output per visited element; produces a transformed array.filter: includes values whose predicate is truthy.reduce: folds values into an accumulator; an initial value avoids empty-array errors and accumulator type ambiguity.find/findIndex: return the first match and stop.some/every: test existence/all-match and short-circuit.forEach: runs a callback for side effects and returns undefined.
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
- When use map rather than forEach? When each visited element should produce a transformed result array.
- Which methods short-circuit?
find,some, andeveryamong common methods. - Why give reduce an initial value? It defines accumulator type and handles empty arrays.
- Does filter deep-copy its elements? No, it returns references to selected values.