Hint
Functions that take other functions as arguments, or return functions as results
A Higher-Order Function is a function that either:
// Takes a function as argument
[1, 2, 3].map(x => x * 2); // map is HOF — takes callback
[1, 2, 3].filter(x => x > 1); // filter is HOF
setTimeout(() => console.log('hi'), 1000); // HOF
// Returns a function
function multiplier(factor) {
return (n) => n * factor; // returns a new function
}
const double = multiplier(2);
const triple = multiplier(3);
double(5); // 10
triple(5); // 15
// Does both (debounce)
function debounce(fn, delay) {
let timer;
return (...args) => { // returns function
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay); // takes fn
};
}
HOFs are foundational to functional programming, enabling code reuse, composition, and abstractions without mutation.