Caffeinated Coder
Book an appointment
Lesson 13 · How JavaScript really works

Scope and closures

Scope decides where a name can be used: every function call gets its own box of variables, linked to the box outside it, and a lookup only ever walks outward. A function remembers the scope it was created in, so a variable can outlive the call that made it. Follow an eleven-line counter as makeCounter() builds a scope, hands back a closure, and that closure keeps count alive from one call to the next.

12 min10 steps5 questionsIntermediate
Fig 13 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.