What Are Higher-Order Functions in JavaScript?
A higher-order function takes or returns a function. Here is what they are and why they matter.
What Are Higher-Order Functions in JavaScript?
A higher-order function is a function that takes a function as an argument, returns a function, or both. This is possible because functions are first-class citizens in JavaScript.
Functions That Take Functions (Callbacks)
[1, 2, 3].map((n) => n * 2); // [2, 4, 6] [1, 2, 3].filter((n) => n > 1); // [2, 3] [1, 2, 3].reduce((a, b) => a + b); // 6 `` `map`, `filter`, and `reduce` are higher-order functions: they take a callback. ### Functions That Return Functions (Factories) ```js function multiplier(factor) { return (n) => n * factor; } const double = multiplier(2); double(5); // 10 `` `multiplier` is a higher-order function: it returns a new function. ### Both ```js function compose(f, g) { return (x) => f(g(x)); } const addOne = (n) => n + 1; const double = (n) => n * 2; const addOneThenDouble = compose(double, addOne); addOneThenDouble(5); // 12 `` `compose` takes two functions and returns a new function. ### Why Higher-Order Functions Matter - **Abstraction**: hide details, express intent. - **Reusability**: write one function, use it with many callbacks. - **Composition**: chain small functions into complex pipelines. - **Functional programming**: pure functions, immutability, composition. ### Common Built-in HOFs - Array: `map`, `filter`, `reduce`, `forEach`, `find`, `some`, `every`, `sort`, `flatMap`. - Function: `bind`, `call`, `apply`. - Timers: `setTimeout`, `setInterval` (take callbacks). - Promise: `then`, `catch`, `finally`. ### The Takeaway A higher-order function takes or returns a function. `map`/`filter`/`reduce` take callbacks. `multiplier`/`compose`/`curry` return functions. HOFs enable abstraction, reusability, composition, and functional programming.
A function that takes a function as an argument, returns a function, or both. This is possible because functions are first-class citizens in JavaScript.
Yes. They take a callback function as an argument. map transforms each element, filter selects elements, and reduce combines elements into a single value.
Yes. A function that returns another function is a higher-order function (a function factory). The returned function often closes over the outer function's variables, creating a closure.
They enable abstraction (hide details), reusability (write one function, use it with many callbacks), composition (chain small functions into pipelines), and functional programming (pure functions, immutability).
Array methods: map, filter, reduce, forEach, find, some, every, sort, flatMap. Function methods: bind, call, apply. Timers: setTimeout, setInterval. Promise methods: then, catch, finally.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

