Do two calls to the same outer function share closed-over variables in JavaScript?
No. Each call creates a separate lexical environment. So makeAdder(5) and makeAdder(10) each have their own x. The closures are independent and do not share state.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How Closures Work With Lexical Scope
Closures are a consequence of lexical scope. Functions store a reference to their lexical environment at creation time. When called later, the outer reference is the stored one, so the function can access outer variables even after the outer function has returned.
Because the inner function holds a reference to the outer lexical environment. As long as the inner function exists, the garbage collector cannot free the environment. The scope chain persists.
At creation time. The function object stores a reference to the lexical environment where it was defined. This reference does not change based on where the function is later called.
Still have questions?
Browse all our FAQs or reach out to our support team
