A higher-order function accepts another function as an argument, returns a function, or both. It treats behavior as a value, allowing reusable code to abstract over what changes. section 10.10 section 10.15
The practical payoff is replacing repeated low-level structure with a reusable context. Instead of rewriting each array loop, error-handling block, or copy-on-write routine, pass the varying operation as a callback. section 10.13 section 14.21
Common uses include:
- Iterate over arrays with forEach(), map(), filter(), and reduce(), passing the operation or selection rule as a function. section 12.9 section 12.6
- Create reusable execution contexts, such as running a callback inside try/catch for consistent error logging. section 10.13
- Transform one function into another with added behavior, as wrapLogging() does while preserving the original operation. section 11.4
- Apply operations to object values, including nested data, while centralizing copy-on-write behavior. section 14.3 section 14.21
Replacing a repeated loop
Start with a fixed loop, then expose its changing behavior: function operateOnArray(array, f) { for (var i = 0; i < array.length; i++) { f(array[i]); } } Calling operateOnArray(foods, cookAndEat) separates the reusable iteration context from the operation. Naming the general function forEach() makes the pattern easier to reuse. section 10.10
Returning a function
A higher-order function can manufacture specialized functions: function makeAdder(n) { return function(x) { return n + x; }; } var plus10 = makeAdder(10); plus10(12) produces 22. The returned function behaves like an ordinary function after receiving its captured configuration. section 11.4
A useful refactoring path is to identify the repeated before-and-after structure, extract it into a function, and replace the differing body with a callback. Higher-order functions reduce duplication, but excessive abstraction can cost readability. section 10.1 section 11.6













