Yes. A closure keeps a reference to its lexical environment. As long as the closure exists, the closed-over variables cannot be garbage collected. This is a common source of memory leaks.
Can closures prevent garbage collection in JavaScript?
More FAQs in Garbage Collection and Block Scope in JavaScript
When a block ends, its lexical environment is eligible for GC (unless a closure keeps it alive). This lets the GC free block-scoped variables sooner than function-scoped var variables, which stay alive until the function returns.
Because var is function-scoped, not block-scoped. A var inside a block leaks to the enclosing function scope and stays alive until the function returns, even if the block has ended. let and const are freed when the block ends.
Remove handlers when no longer needed, do not close over large objects unless necessary, use WeakRef or WeakMap where appropriate, and keep closures short-lived. Avoid accumulating closures that each keep a large object alive.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.