🔒JavaScript Closures Demystified
Build one mental model for closures that covers the setTimeout loop bug, private state, and React hooks — then ship a tiny state container using nothing but functions and lexical scope.
Phase 1Lexical Scope and Captured Environments
Build intuition for lexical scope and captured variables
A function remembers where it was born
6 minA function remembers where it was born
Variables are looked up, not copied
6 minVariables are looked up, not copied
let creates a new room every time
6 minlet creates a new room every time
Returning a function is returning its scope
7 minReturning a function is returning its scope
Phase 2Fixing the Classic Loop Bug
Solve the setTimeout loop bug and IIFE variations
Why setTimeout prints the last number
7 minWhy setTimeout prints the last number
The one-word fix that changes everything
6 minThe one-word fix that changes everything
Methods that hide the scope fix for you
6 minMethods that hide the scope fix for you
The same bug, wearing an event handler
7 minThe same bug, wearing an event handler
Snapshots using function parameters
7 minSnapshots using function parameters
Phase 3Modules, Private State, and Hooks
See closures behind modules, private state, and hooks
Pre-class JavaScript already had private fields
7 minPre-class JavaScript already had private fields
useState is a closure in disguise
8 minuseState is a closure in disguise
A one-line cache using captured scope
7 minA one-line cache using captured scope
Subscribers live in their parent's scope
7 minSubscribers live in their parent's scope
Phase 4Build a Closure-Only State Container
Ship a tiny state container built from closures only
Build a Redux-style store from scratch
20 minBuild a Redux-style store from scratch
Frequently asked questions
- What exactly is a closure in JavaScript?
- This is covered in the “JavaScript Closures Demystified” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- Why does the setTimeout-in-a-loop bug happen with var?
- This is covered in the “JavaScript Closures Demystified” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- How do closures create private state without classes?
- This is covered in the “JavaScript Closures Demystified” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- Why does React's useState rely on closures?
- This is covered in the “JavaScript Closures Demystified” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
- Do closures leak memory in JavaScript?
- This is covered in the “JavaScript Closures Demystified” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Related paths
🐳Docker Containers Basics
Build the mental model first, then the commands — from containers vs VMs through images, layers, volumes, and networking to composing a multi-service app.
🧪Property-Based Testing
Go beyond example-based tests — learn to express what your code should always do, then let a framework find the inputs that break it.
🦀Rust's Ownership Model
Build a working mental model of Rust's ownership system — from stack vs heap intuition through borrow checker mastery — so you can read and write Rust without fighting the compiler.
💻Elixir Pattern Matching
Stop reading `=` as assignment and start using it as Elixir's core flow-control tool — through function heads, guards, and `with` — until you can rewrite a tiny command parser without a single `if`.