The Relationship Between Scope and Closures in JavaScript
Closures are a consequence of lexical scope. Here is how scope makes closures possible.
The Relationship Between Scope and Closures in JavaScript
Closures are a direct consequence of lexical scope. You cannot understand closures without understanding scope first.
What a Closure Is
A closure is a function that remembers the variables from its lexical scope, even after that scope has finished executing. It "closes over" those variables.
function makeCounter() { let count = 0; return () => ++count; } const counter = makeCounter(); counter(); // 1 counter(); // 2 `` After `makeCounter` returns, its execution context is gone. But `count` persists because the returned arrow keeps a reference to `makeCounter`'s lexical environment. ### Why Scope Makes This Possible The returned function was **written inside** `makeCounter`. Its outer environment reference points to `makeCounter`'s environment. That reference is part of the function object itself. When the function is called later, the engine walks the scope chain and finds `count`. ### The Scope Chain Persists Normally, when a function returns, its local variables are eligible for garbage collection. But if an inner function keeps a reference to the outer environment, the variables survive. The closure keeps them alive. ```js function makeAdder(x) { return function (y) { return x + y; }; } const add5 = makeAdder(5); const add10 = makeAdder(10); add5(3); // 8 add10(3); // 13 `` `add5` and `add10` each have their own `x` (5 and 10), because each call to `makeAdder` created a separate lexical environment. ### Closures Are Not Just Functions A closure is the combination of the function and its lexical environment. The function alone is not the closure; the function **plus** the variables it closes over is the closure. ### Common Closure Patterns - **Data privacy**: hide variables behind a returned API. - **Partial application**: preset some arguments. - **Memoization**: cache results in a closed-over variable. - **Event handlers**: handlers that remember their setup context. ### The Takeaway Closures are a consequence of lexical scope. A function defined inside another keeps a reference to the outer lexical environment. When the outer function returns, the inner function still has access to those variables. The scope chain persists because the function object holds the outer reference.
Closures are a consequence of lexical scope. A function defined inside another keeps a reference to the outer lexical environment. When the outer function returns, the inner function still has access to those variables via the scope chain.
Because the inner function holds a reference to the outer lexical environment. As long as the inner function exists, the environment (and its variables) cannot be garbage collected. The scope chain persists.
No. A closure is the combination of a function and its lexical environment (the variables it closes over). The function alone is not the closure; the function plus the retained outer variables is the closure.
No. Each call creates a separate lexical environment. So makeAdder(5) and makeAdder(10) each have their own x. The closures are independent.
Data privacy (hide variables behind a returned API), partial application (preset arguments), memoization (cache results), event handlers (remember setup context), and function factories like makeAdder.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

