Back to feed
Dev.to
Dev.to
6/26/2026
# Understanding JavaScript Closures Through Call Stack, Heap Memory & `[[Scopes]]`

# Understanding JavaScript Closures Through Call Stack, Heap Memory & `[[Scopes]]`

Short summary

JavaScript closures preserve variables from outer scopes in heap memory through a hidden [[Scopes]] reference, allowing inner functions to access them even after the parent function returns. The mechanism combines call stack cleanup with lexical environment persistence. Understanding this reveals closures as the engine's solution for keeping needed data alive.

  • Captured variables are stored in heap-allocated lexical environments that persist after the outer function returns
  • The [[Scopes]] hidden reference enables functions to access these captured variables through the scope chain
  • Variable lookup follows a chain: local scope → lexical environment → parent scopes until found

Generated with AI, which can make mistakes.

Is this a good recommendation for you?

Comments

Failed to load comments. Please try again.

Explore more