JavaScript, inside out · Chapter 21 of 51
Closures
By the end of this chapter you can make a function that remembers values between calls, keep those values private, and wrap one function inside another to give it a memory. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The order flow
- 5 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Kabir has found Tapri's token machine keeping its last number in a variable that any code in the file can change. Meera wants a UPI wallet for regulars whose balance nothing can touch except paying and topping up. Anjali's welcome offer must apply once, not on every visit. Ravi's price lookups for big office orders are slow, and the same orders come every day. And the order announcer calls out every order as undefined.
With what you know so far, a value that lives between calls must live outside the function, in the file, where every other function can change it too, by mistake or on purpose. And there is only one of it: two token machines would share one count.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
makeTokenCounter: a token counter that remembers its count
Write makeTokenCounter(). It returns a function, and each call of that function returns the next token number: 1 the first time, then 2, then 3, and so on.
Every call of makeTokenCounter starts a separate count from 1: with two counters, calling one never changes the other.
- Task 2
makeWallet: a wallet whose balance nobody can touch
Write makeWallet(start). It returns an object with three functions that share a balance starting at start:
pay(amount) takes the amount off and returns true, or returns false and changes nothing when the amount is more than the balance (paying exactly the balance is allowed). topUp(amount) adds the amount and returns the new balance. balance() returns the balance.
The balance must not be a property of the object: the wallet object holds only these three functions, and setting a property on it from outside never changes the real balance. Each wallet has its own balance.
- Task 3
once: a welcome offer that applies only once
Write once(fn). It returns a new function. The first time that function is called, it calls fn with the same arguments and returns fn's result. Every later call returns that first result again without calling fn at all, whatever arguments it gets.
Each call of once makes its own wrapper with its own memory, so wrapping the same fn twice gives two wrappers that each run fn once.
- Task 4
memoize: remember prices already worked out
Write memoize(fn), where fn takes one argument. It returns a new function that gives the same result as fn, but works out each different argument only once: the first call with an argument calls fn and saves the result, and every later call with that argument returns the saved result without calling fn.
A saved result that is falsy, such as 0, is still a saved result and must not call fn again. Each call of memoize has its own store of results.
- Task 5 · interview question
Interview: announceOrders, fix the announcer that says undefined
announceOrders(orders, say) is broken now: every announcement says undefined. Fix it so it calls say with the first order after 1 second, with the second order after 2 seconds, and so on, one order per second, in order. Nothing is said straight away.
What you will learn
- Closures: a function made inside another keeps the outer function's variables alive after that call has finished.
- Private state: functions that share variables nobody else can reach.
- Wrapping a function: a new function that calls the old one, with a memory of its own.
- Closures in loops: which variable each function remembers.
More in The order flow
Build "Closures" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.