compose(f, g) returns a function that closes over f and g. Each call to the returned function invokes f(g(x)). The closure keeps f and g alive so the returned function can use them later.
Closures and Higher-Order Functions in JavaScript
How does the compose function use closures in JavaScript?
The answer
More FAQs in Closures and Higher-Order Functions in JavaScript
When a HOF takes a callback, the callback closes over variables from where it was defined. When a HOF returns a function, the returned function closes over the HOF's local variables. Closures are what make HOFs work.
The callback passed to map closes over variables from where the callback was defined. For example, [1, 2, 3].map(n => n * multiplier) works because the arrow closes over the outer multiplier variable.
Yes. They take a callback function as an argument, which makes them higher-order. The callbacks close over outer variables, which is why you can reference outer state inside them.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.