Skip to content
YouTurkey Tech
All JavaScript topics

Higher-Order Functions

Beginner

Functions are values: you can pass one to another function, or get one back as a result.Builds on Scope Chain

Call stack

Empty

Scopes

Empty

Heap

Empty

Console

Empty

0/18Press Play, or step through with the arrows, to begin.
Step 0 of 18

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. Writing double() 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
  1. Step 1:

    The global context starts in its creation phase and stores every declaration before any line runs.

  2. Step 2:

    double and applyTwice become function objects in the heap. A function is a value, like a number or an object.

    Lines 1–7

  3. Step 3:

    multiplier is stored the same way, and triple is reserved but uninitialized, as every const starts.

    Lines 11–15

  4. Step 4:

    applyTwice(double, 3) passes double without calling it: no parentheses. The parameter fn now points to the same function object.

    Line 9

  5. Step 5:

    A function that takes a function is a higher-order function. Inside, fn is looked up and leads to double.

    Line 6

  6. Step 6:

    The inner fn(value) runs first: double is called with 3. Its environment links to global, where double was written.

    Line 6

  7. Step 7:

    double returns 6 and its context leaves the stack.

    Line 2

  8. Step 8:

    The outer fn(…) calls double again, this time with 6.

    Line 6

  9. Step 9:

    double returns 12.

    Line 2

  10. Step 10:

    applyTwice returns 12 and console.log prints it. Pass a different function and the same applyTwice does something else.

    Lines 6–9

  11. Step 11:

    Now the other kind: multiplier(3) is called, with factor set to 3.

    Line 15

  12. Step 12:

    The arrow function is created inside multiplier, so it remembers multiplier's environment.

    Line 12

  13. Step 13:

    A function that returns a function is a higher-order function too. triple now holds the arrow function.

    Line 15

  14. Step 14:

    multiplier's call has ended, but its environment stays: the arrow function still needs factor. This is a closure.

    Line 15

  15. Step 15:

    triple(5) runs the arrow function. Its environment links to multiplier's, where it was created.

    Line 16

  16. Step 16:

    n is found in its own environment. factor is not, so the lookup moves out and finds 3.

    Line 12

  17. Step 17:

    The arrow function returns 15, and console.log prints it.

    Lines 12–16

  18. Step 18:

    Two shapes, one idea: applyTwice takes a function, multiplier returns one. map, filter and setTimeout work the same way.