JavaScript: Higher-Order Functions (In-Depth)

A higher-order function is a function that either receives another function as an argument, returns a function, or both. They are foundational in JavaScript's functional programming style.

What is a Higher-Order Function?

Example: Passing Functions as Arguments

function repeat(n, action) {
  for (let i = 0; i < n; i++) {
    action(i);
  }
}
repeat(3, console.log);
    
// Output:
0
1
2

Example: Returning Functions

function multiplier(x) {
  return function(y) {
    return x * y;
  }
}
const double = multiplier(2);
double(5); // 10
    
// Output:
10

Common Built-In Higher-Order Functions

Why Use Higher-Order Functions?

Practice: Try writing a custom filter or map higher-order function to solidify your understanding!