Stack vs Heap
The stack holds function frames (return addresses and locals). The heap holds objects, arrays, closures' environments, and anything that outlives a single frame. JavaScript values live in one or the other according to the engine, not according to a keyword you type.
Detailed Explanation
You do not write malloc in JavaScript. You still pay for the two regions every compiled language uses.
The call stack grows when you call a function and shrinks when it returns. A frame stores primitives that the engine can keep unboxed (this, small integers, the return IP) and pointers to heap values. The stack is bounded. Recursion that never returns throws RangeError. Stack memory is cheap to allocate (bump a pointer) and is freed in order.
The heap is the pool where {}, [], functions, strings (in most cases), and environments live. new, object literals, and [] allocate here. The garbage collector reclaims heap objects that are not reachable from roots: the stack, global/module bindings, and pending callbacks.
Primitives (number, boolean, undefined, null, symbol, bigint, small strings) may be stored directly in a frame or inlined in an object. The spec does not say. What you can rely on: assigning a number copies the value; assigning an object copies the reference. Two names for one object share one heap allocation.
Escape analysis in a JIT can keep a short-lived object on the stack or in registers if it never leaves the function. You cannot force this. You can make it more likely by not returning the object and not storing it on a long-lived parent.
"Heap" in data structures (binary heap) is a different word. The GC heap is a memory region.
What it costs you: confusing the two when debugging a leak. A growing stack is recursion. A growing heap is retained objects (caches, listeners, detached DOM, closures). process.memoryUsage() in Node and the browser Performance panel show heap, not "how deep is my stack".
Reach for this distinction when you read a memory profile, when you convert recursion to a loop (stack → heap-allocated array), and when you explain why const x = { n: 1 } is not "on the stack" in any portable sense.
Code Examples
function frame(n) {
const local = n + 1; // likely a stack slot
return local;
}
function heapAlloc() {
const user = { id: 7 }; // heap object
return user; // reference leaves the frame; object stays
}
const a = heapAlloc();
const b = a; // same heap object
b.id = 8;
console.log(a.id); // => 8
console.log(frame(1)); // => 2function stackOverflow(n) {
return stackOverflow(n + 1);
}
try {
stackOverflow(0);
} catch (err) {
console.log(err.name); // => RangeError
}Common Gotchas
There is no `stack` or `heap` keyword
allocation is implied by creating objects and calling functions.
Closures move "locals" onto the heap
a returned function keeps an environment object, so `n` in `makeCounter` is not freed when `makeCounter` returns.
Shared references are heap identity
`===` on objects is pointer equality.
A binary heap (data structure) is an array on the GC heap
unrelated to stack vs heap allocation.