A higher-order function takes another function as an argument, returns a function, or does both. JavaScript supports this because functions are first-class objects: they can be stored, passed around, and returned like other values. section 2.3
Passing behavior into a function
Instead of hard-coding what to do with each person, pass the action, selector, and printer as functions: function printPeople(people, selector, printer) { people.forEach(function (person) { if (selector(person)) printer(person); }); } var inUs = person => person.address.country === 'US'; printPeople(people, inUs, console.log); This separates iteration from the decision and output, making the operation more reusable. section 2.3
Common higher-order patterns include:
- Accept a function as an argument, as with applyOperation(2, 3, multiplier). section 2.3
- Return a new function, as with add(3)(3), which specializes addition with a saved value. section 2.3
- Compose or chain functions to express independent processing steps, such as filtering enrollment records and averaging grades. section 1.3
Listing 1.5. Programming with function chains
_.chain(enrollment)
.filter(student => student.enrolled > 1)
.pluck('grade')
.average()
.value(); //-> 90 #1
A function chain shows higher-order operations transforming enrollment data through filtering, projection, and averaging. section 1.3













