JavaScript Closures and Scope Exercises
Learn JavaScript closures and scope step by step with interactive exercises covering scope chains, function and block scope, lexical lookup, shadowing, saved values, private state, factories, memoization, modules, and closure-based guards. Write and test real code in your browser with immediate feedback.
Exercises
Work from beginner to hard, or use the filters to find a specific skill.
Scope Chain Lookup
The scope chain is the path JavaScript follows when code reads a variable name. ...
Function Scope
Function scope means variables declared inside a function belong to that functio...
Variable Shadowing
Variable shadowing happens when an inner scope declares a name that also exists ...
Block Scope
Block scope means a `let` or `const` variable declared inside braces belongs to ...
Lexical Scope
Lexical scope means a function's variable access is determined by where the func...
Closure Over Configuration
A closure forms when an inner function keeps access to a variable from an outer ...
Private State With Closures
A closure can keep state alive without putting that state in the global scope. E...
Saved Values
Closures are often used to split setup from use. The outer function receives a v...
Loop Closures
Closures capture variables, not snapshots of values. With `var), a loop has one ...
Stateful Closures
A closure can remember how many times something has happened. That state lives b...
Private Collections
Closures can protect collection state by keeping the actual array inside an oute...
Once Wrappers
A once wrapper is a closure that controls whether another function is allowed to...
Memoization
Memoization uses a closure to remember previous inputs and results. The wrapped ...
Shared Closure State
Multiple functions returned from the same outer function close over the same var...
Function Factories
A function factory creates functions that carry their own closed-over configurat...
Accidental Shared State
Closures are useful because they keep state alive, but the location of that stat...
Keyed Memoization
Implement keyed memoization with JavaScript closures, a private cache, and a key...
Closure State Machines
Build a JavaScript closure-based history store with private current state and un...
Closure Modules
Build a subscription hub with JavaScript closures, private listener state, and s...
Closure-Based Guards
Model a rate-limit token bucket with JavaScript closure state, capacity rules, r...
Follow where functions find and remember values
A JavaScript closure is a function that keeps access to bindings from the lexical environment where it was created. These JavaScript closure exercises make that idea visible by asking you to trace where a value comes from, what survives after an outer function returns, and which calls share or isolate state.
The collection begins with lexical scope practice: scope-chain lookup, function scope, block scope, variable shadowing, and the rule that JavaScript resolves names from where code is written. It then introduces closures over configuration, saved values, loop bindings, counters, and private state in small steps.
Later JavaScript practice problems for private state cover private collections, once-only wrappers, memoization, function factories, accidental shared state, keyed caches, closure state machines, module-style APIs, and guards. These patterns show why closures matter in real code: they let functions remember data without exposing it as a global variable.
- Exercises
- 20
- Difficulty
- Beginner to Hard
- Good to know first
- Be confident declaring and calling functions and reading nested function bodies.
What you’ll practice
- Trace scope-chain lookup through nested functions
- Distinguish global, function, and block scope
- Recognize lexical scope and variable shadowing
- Understand how closures preserve saved values
- Create private counters, collections, and stateful functions
- Build function factories and once-only wrappers
- Use closure state for memoization and keyed caches
- Design module APIs, state machines, and closure-based guards
Who these exercises are for
This practice set is for learners who can already declare and call functions but find scope or closures difficult to visualize. It also suits developers who want focused lexical-scope practice and stronger intuition for private state, factories, memoization, and module patterns.
How practice works
- Write and run JavaScript directly in your browser
- Use instant test feedback to trace captured and shadowed values
- Progress from scope lookup to practical closure patterns
- Filter exercises by difficulty and save your progress when signed in