Functions that Use Functions
A higher-order function takes another function as an argument or returns a function.
This enables reusable, composable logic.
JavaScript Tutorial
Higher-order functions work with other functions as inputs or outputs.
They are central to functional programming and modern JavaScript style.
They let you build reusable behaviors and avoid repetitive loops.
Understanding them makes callbacks, array methods, and composition easier.
function apply(value, fn) { ... }
const factory = () => (value) => value;function apply(value, fn) {
return fn(value);
}
console.log(apply(5, (n) => n * 2));A function can receive another function as input.
function makeMultiplier(factor) {
return (n) => n * factor;
}
const triple = makeMultiplier(3);
console.log(triple(4));Return a function configured with a value.
A higher-order function takes another function as an argument or returns a function.
This enables reusable, composable logic.
Common higher-order functions include map, filter, and reduce.
They are expressive and reduce boilerplate loops.
Returning a function lets you create specialized behavior.
This is useful for configuration and reuse.
Without
const nums = [1, 2, 3];
const doubled = [];
for (const n of nums) {
doubled.push(n * 2);
}With
const nums = [1, 2, 3];
const doubled = nums.map((n) => n * 2);Use higher-order functions when they improve clarity, not by default.
Be mindful of extra iterations in tight loops.
Name functions if callbacks get complex.
Taking a function or returning one.
Array methods like map or filter.
To create specialized functions with shared configuration.
Practice: Write a function that accepts a number and a formatter callback.
// TODO: function formatNumber(n, formatter)
One Possible Solution
function formatNumber(n, formatter) {
return formatter(n);
}
console.log(formatNumber(12.3, (v) => v.toFixed(1)));A function that takes or returns another function.
Yes, map, filter, and reduce all take callbacks.
They help write reusable and composable logic.
Try passing different callbacks to see how results change.