🟢 EasyFunctions📖 Theory Question

What are Higher-Order Functions (HOF)?

💡

Hint

Functions that take other functions as arguments, or return functions as results

Full Answer

A Higher-Order Function is a function that either:

  • Accepts a function as an argument, OR
  • Returns a function as its result (or both)
// 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.

💡 map, filter, reduce, forEach, addEventListener, setTimeout — all HOFs you use every day without realizing it.

More Functions Questions

🟢 EasyWhat is the difference between call, apply, and bind?→🟢 EasyHow do arrow functions differ from regular functions?→🟢 EasyWhat is a pure function and why does it matter?→🟢 EasyWhat is an IIFE (Immediately Invoked Function Expression) and when do you use it?→

Practice this in a timed sprint →

5 free questions, no signup required

âš¡ Start Sprint