Higher-Order Functions
BeginnerFunctions are values: you can pass one to another function, or get one back as a result.Builds on Scope Chain
Empty
Empty
Empty
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What is a higher-order function in JavaScript?
In JavaScript a function is a value. You can store it in a variable, pass it to another function, or return it from one. A is a function that does at least one of the last two: it takes a function as an argument, or it returns a function.
You already use them: array.map(fn), array.filter(fn), addEventListener('click', fn) and
setTimeout(fn, 1000) all take a function. The function you pass is called a callback.
How do functions work as values?
- The function object lives in the heap. Its name is just a variable that points at it.
- Passing
double(no parentheses) passes that reference. Writingdouble()calls it and passes the result instead. - A function remembers the scope where it was created, not where it is called. A function returned from another one keeps the outer function's variables alive: that is a closure.
What mistakes do people make with higher-order functions?
Mini example
const prices = [100, 250, 40]; const withTax = prices.map((price) => price * 1.14);const cheap = prices.filter((price) => price < 200); function greeter(greeting) { return (name) => `${greeting}, ${name}!`;} const sayHello = greeter('Hello');console.log(sayHello('Mona')); // Hello, Mona!Quick check
What does setTimeout(greet(), 1000) pass to setTimeout?
Further reading
Watch the video
The full explanation from the YouTurkey Tech YouTube channel, in Arabic.
All steps as text
- Step 1:
The global context starts in its creation phase and stores every declaration before any line runs.
- Step 2:
doubleandapplyTwicebecome function objects in the heap. A function is a value, like a number or an object.Lines 1–7
- Step 3:
multiplieris stored the same way, andtripleis reserved but uninitialized, as everyconststarts.Lines 11–15
- Step 4:
applyTwice(double, 3)passesdoublewithout calling it: no parentheses. The parameterfnnow points to the same function object.Line 9
- Step 5:
A function that takes a function is a higher-order function. Inside,
fnis looked up and leads todouble.Line 6
- Step 6:
The inner
fn(value)runs first:doubleis called with 3. Its environment links to global, wheredoublewas written.Line 6
- Step 7:
doublereturns 6 and its context leaves the stack.Line 2
- Step 8:
The outer
fn(…)callsdoubleagain, this time with 6.Line 6
- Step 9:
doublereturns 12.Line 2
- Step 10:
applyTwicereturns 12 and console.log prints it. Pass a different function and the sameapplyTwicedoes something else.Lines 6–9
- Step 11:
Now the other kind:
multiplier(3)is called, withfactorset to 3.Line 15
- Step 12:
The arrow function is created inside
multiplier, so it remembers multiplier's environment.Line 12
- Step 13:
A function that returns a function is a higher-order function too.
triplenow holds the arrow function.Line 15
- Step 14:
multiplier's call has ended, but its environment stays: the arrow function still needs
factor. This is a closure.Line 15
- Step 15:
triple(5)runs the arrow function. Its environment links to multiplier's, where it was created.Line 16
- Step 16:
nis found in its own environment.factoris not, so the lookup moves out and finds 3.Line 12
- Step 17:
The arrow function returns 15, and console.log prints it.
Lines 12–16
- Step 18:
Two shapes, one idea:
applyTwicetakes a function,multiplierreturns one. map, filter and setTimeout work the same way.