Home/Exercises/Closures & Scope
exercise set · 20 challenges · beginner to hard

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.

Choose your next challenge

Exercises

Work from beginner to hard, or use the filters to find a specific skill.

20exercises
About this practice set

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.
Start with Scope Chain Lookup

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