foundation

Higher-order functions

Callbacks, map/filter/reduce, function factories, decorators, and dependency injection seams.

A higher-order function takes a function, returns a function, or both. `map`, `filter`, `reduce`, middleware, validators, and decorators separate policy from iteration plumbing.

When wrapping functions, preserve `this`, arity, and error behavior intentionally. `reduce` becomes unreadable when it encodes too much state — prefer explicit loops when clarity wins.

					const withLogging = (fn) => (...args) => {
  console.log('call', args);
  return fn(...args);
};
				

On interviews, explain the concept with a concrete example and name the runtime behavior interviewers probe.

Common pitfalls include mixing similar APIs and forgetting edge cases during live coding.

The trade-off is often clarity versus performance or safety versus convenience.

Checklist:

  • Name input/output contracts.
  • Preserve this when wrapping.
  • Keep reducers focused.