What are higher-order functions and why do they matter?
Take a moment.
Say your answer out loud or write it down first — retrieval is what makes it stick. Reveal when you're ready.
Short answer
A higher-order function either takes functions as arguments, returns a function, or both. They enable reusable abstractions like map, filter, setTimeout and middleware pipelines.
Detailed explanation
Because JavaScript treats functions as first-class citizens — assignable to variables, passable as arguments, returnable from other functions — you can build logic that operates on other logic. map(fn), filter(fn) and reduce(fn) parameterize *what to do* separately from *how to iterate*. Event systems (addEventListener), timers (setTimeout), promises (.then) and React components receiving render props are all higher-order patterns. Returning functions unlocks factories and partial application, e.g., multiplyBy(2) producing specialized functions. This style tends to reduce duplication, make code more declarative, and compose small behaviors into bigger ones — a mindset interviewers want to see because it transfers directly to hooks, HOCs, Express middleware and RxJS-style streams.
Example
const numbers = [1, 2, 3, 4];
numbers.map(n => n * n) // takes a function
.filter(n => n % 2 === 0)
.reduce((sum, n) => sum + n, 0); // 20
// Returning a function:
const multiplyBy = factor => n => n * factor;
const double = multiplyBy(2);
double(21); // 42Interview tip
Name the two forms explicitly (accepts / returns functions), give map as your canonical example, and connect to something framework-level like addEventListener or middleware.
Common mistakes
- Defining them vaguely as 'functions about functions' without the two concrete forms.
- Failing to name any real-world examples beyond map/filter.