Facebook Pixel

How JavaScript Allocates Memory for Variables and Functions

JS reserves memory for declarations before running code. Here is what gets allocated, where it lives, and how the heap fits in.

How JavaScript Allocates Memory for Variables and Functions

Memory in JavaScript is split into two regions: the stack and the heap. Understanding where things live explains a lot about behavior.

Stack vs Heap

  • Call Stack: stores primitive values and references. Fast, LIFO, fixed-size frames per function call.
  • Heap: stores objects, arrays, and functions. Unstructured, managed by the garbage collector.

What Goes Where

  • Primitives (number, string, boolean, null, undefined, symbol, bigint) are stored directly where the variable lives, often on the stack.
  • Objects, arrays, and functions are stored on the heap. The variable holds a reference (a pointer) to that heap location.

Assignment Behavior

let a = 5; let b = a; // b is a copy, a and b are independent b = 10; console.log(a); // 5 let obj1 = { x: 1 }; let obj2 = obj1; // obj2 points to the same object obj2.x = 99; console.log(obj1.x); // 99

Primitives copy by value. Objects copy by reference. This is the root of many bugs.

Function Memory

Function declarations are stored in full during the memory allocation phase. Their body lives in memory. Function expressions follow variable rules: var gets undefined, let/const enters TDZ.

Garbage Collection

When no references to a heap object remain, it becomes unreachable. The garbage collector (V8 uses mark-and-sweep with the Orinoco collector) frees that memory. Closures can keep objects alive longer than expected.

The Takeaway

Primitives live on the stack and copy by value. Objects live on the heap and copy by reference. Function declarations are stored in full during memory allocation. Garbage collection reclaims unreachable heap memory.

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.

When an object on the heap has no references pointing to it, it becomes unreachable. The garbage collector uses mark-and-sweep to identify and free such objects.

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.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.