Can closures prevent garbage collection?
Yes. A closure that captures a variable keeps a reference to it, so the captured variable and any objects it references cannot be garbage collected as long as the closure exists.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How JavaScript Allocates Memory for Variables and Functions
Objects, arrays, and functions are stored on the heap. The variable holds a reference (pointer) to that heap location, which is why copying a variable shares the same object.
Primitives like number, string, and boolean are stored directly where the variable lives, typically on the call stack. They copy by value on assignment.
Primitives copy by value, so changes to one variable do not affect the other. Objects copy by reference, so two variables can point to the same object and mutations are visible through both.
Still have questions?
Browse all our FAQs or reach out to our support team
