Sub topic 17
Returning Functions
This chapter assumes you can declare functions, write arrow functions with implicit returns, and give parameters default values. You've seen functions return numbers, strings, arrays, and objects. In JavaScript a function is also a value, so a function can return another function.
A Function That Builds Functions
A function that returns another function is called a factory. The outer function takes some configuration. The inner function does the actual work when you call it later.
function makeMultiplier(factor = 2) {
return (n) => n * factor;
}
const triple = makeMultiplier(3);
console.log(triple(5)); // 15
console.log(makeMultiplier()(5)); // 10makeMultiplier(3) doesn't multiply anything yet. It returns a new arrow function, and triple holds that function. Calling triple(5) runs the inner function.
makeMultiplier()(5) does both steps in one expression. The first pair of parentheses calls the factory, which uses the default factor of 2. The second pair calls the function the factory returned.
How the Inner Function Remembers
When makeMultiplier(3) finishes, its parameter factor would normally go away. The returned arrow function still refers to factor, so JavaScript keeps that binding alive. A closure is a function together with the variables it captured from the scope where it was created.
Each call to the factory creates a fresh parameter binding, so each returned function has its own captured value:
function makeMultiplier(factor = 2) {
return (n) => n * factor;
}
const double = makeMultiplier(2);
const tenTimes = makeMultiplier(10);
console.log(double(4)); // 8
console.log(tenTimes(4)); // 40Captured Variables Stay Live
A closure captures the variable itself, not a copy of its value at the time. If the inner function changes the variable, the next call sees the change:
function makeCounter(start = 0) {
let count = start;
return () => {
count += 1;
return count;
};
}
const next = makeCounter(5);
console.log(next()); // 6
console.log(next()); // 7The arrow function here has a block body, so it needs an explicit return. Without that line, next() would return undefined.
Common Mistakes
| Mistake | Result | Fix |
|---|---|---|
| Calling the factory and forgetting to call the result | You get a function, not a value | Add the second call: `makeMultiplier(3)(5)` |
| Expecting two factory calls to share state | Each call has its own variables | Call the factory once and reuse what it returns |
| Block body without `return` in the inner function | Inner calls return `undefined` | Add `return` or use an expression body |
Exercise
Given function makeAdder(x) { return (y) => x + y; }, what does makeAdder(4)(makeAdder(1)(2)) evaluate to, and why?
Show answer
7. The inner expression makeAdder(1)(2) returns 1 + 2 = 3. Then makeAdder(4) returns a function that adds 4, and calling it with 3 gives 7. Each call to makeAdder captures its own x.
Exercise
Two counters are created with const a = makeCounter(); const b = makeCounter();. After calling a() twice and b() once, what does a() return next?
Show answer
3. Each makeCounter call creates a separate count variable. Counter a has already reached 2, so its next call returns 3. Calling b does not affect a.