Facebook Pixel

Is a closure just a function in JavaScript?

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.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in The Relationship Between Scope and Closures 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.

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. 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

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.