liveBook · Manning

Higher-order function

As explained in Functional Programming in JavaScript

A higher-order function is a function that takes other functions as arguments, returns a function, or both.

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

How Functional Programming in JavaScript covers it

How deep it goes
Explains passing and returning functions with JavaScript examples, then connects them to composition and chains without developing implementation internals.
Examples
Demonstrates sorting, operation application, returned functions, people filtering, enrollment averages, and reactive SSN validation.
What it assumes
Assumes basic JavaScript functions, arrays, callbacks, and method calls while introducing first-class and higher-order behavior explicitly.
What you can do after
Apply it in JavaScript to separate data traversal from behavior and assemble modular operations with functions such as filter and map.
How it connects
Connects higher-order functions to first-class functions, closures, functional composition, function chains, and functional programming.

Also explained in

Compare how 14 books explain higher-order function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.