Skip to content
Preptima
mediumConceptCodingEntryMidSenior

What is a closure, and how does one end up leaking memory?

4 min readUpdated 2026-08-15

Answer

Short answer

A closure is a function plus the scope it was created in, held by reference rather than copied.It leaks when something long-lived keeps the function alive, because everything the captured scope can reach stays reachable with it.

Keep closures explicit in the answer because that is the concept the interviewer is actually trying to test. A good closures explanation names the trade-off, the failure mode, and the evidence you would use before choosing.

Was this answer helpful?

Likely follow-ups

  • Two callbacks are created side by side and only one of them is stored. What could still be retained, and how would you check?
  • Why does removeEventListener with an inline arrow function fail to remove anything?
  • When would you reach for WeakMap or WeakRef, and what does each one not guarantee?
  • How does a closure over a DOM node keep an entire detached subtree alive?

Related questions

Further reading