Glossary / Heap

Heap

The heap is the conceptual memory area where JavaScript engines allocate objects and other dynamically sized data. Variables and properties hold references to objects rather than embedding full object contents in each binding. The ECMAScript specification describes object behavior, not a required physical memory layout; engines choose allocation, representation, and garbage-collection strategies.

References, reachability, and identity

const original = { details: { enabled: true } };
const alias = original;
alias.details.enabled = false;
console.log(original.details.enabled); // false: same object graph

The heap is often contrasted with the call stack: stack frames represent active calls; heap objects can outlive a call when referenced by globals, closures, modules, host objects, or other heap objects. Reachability from live roots determines whether an object may be reclaimed. Cycles alone are not leaks in tracing garbage collectors; cycles that remain reachable are.

“Heap” does not imply one contiguous region or fixed object representation. Engines may move objects, use generational/incremental collection, optimize allocations, or store some values differently. Do not depend on object addresses, collection timing, or engine-specific layout. Use heap snapshots and allocation profiles to investigate memory behavior rather than assuming a variable’s source-level placement predicts physical storage.

Interview questions

  1. What is the heap used for conceptually? Dynamically allocated objects and data referenced by program values.
  2. Can an object outlive the function that created it? Yes, if a live reference such as a closure retains it.
  3. Are cycles automatically memory leaks? No; tracing GC can collect unreachable cycles.
  4. Does ECMAScript prescribe the heap layout? No, that is implementation-specific.

References