JavaScript Interview Question #116
What is a higher-order function?
Closures, Prototypes, Classes & Advanced Functions Mid-Level Intermediate
Quick Interview Answer
A higher-order function takes one or more functions as arguments, returns a function, or both. JavaScript supports this naturally because functions are first-class values.
Detailed Explanation
A higher-order function takes one or more functions as arguments, returns a function, or both. JavaScript supports this naturally because functions are first-class values.
Common built-in examples include map, filter, and reduce because they receive callback functions. Higher-order functions can improve reuse by separating general control logic from specific behavior.
Code Example
function withLogging(action) {
return (...args) => {
console.log("Arguments:", args);
const result = action(...args);
console.log("Result:", result);
return result;
};
}
const add = (a, b) => a + b;
const loggedAdd = withLogging(add);
loggedAdd(2, 3);