How are scope and closures related in JavaScript?
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.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in The Relationship Between Scope and Closures in JavaScript
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.
Still have questions?
Browse all our FAQs or reach out to our support team
