🔒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
🐍Python Decorators Introduction
Build one mental model for Python decorators that covers closures, argument passing, functools.wraps, and stacking — then ship a working caching or logging decorator from scratch in under 30 lines.
🦀Rust Lifetimes Explained
Stop reading `'a` as line noise and start reading it as scope arithmetic — one failing snippet at a time — until you can thread lifetimes through a small parser or iterator adapter without fighting the borrow checker.
☸️Kubernetes Core Concepts
Stop drowning in 30+ resource types. Build the mental model one primitive at a time -- pods, deployments, services, ingress, config -- then deploy a real app with rolling updates and health checks.
📈Big O Intuition
Stop treating Big O as math you memorized for an interview — build the intuition to spot O(n²) disasters, pick the right data structure without thinking, and rewrite a slow function from O(n²) to O(n) in under five minutes.