Fundamentals 5 min read

Understanding the Difference Between JavaScript Stack and Heap Memory

The article explains how JavaScript engines store variables in stack and heap memory, clarifies why const primitives are immutable while object properties can change, shows that new objects reside in heap, and discusses allocation efficiency and garbage‑collection behavior of both memory regions.

Full-Stack Trendsetter
Full-Stack Trendsetter
Full-Stack Trendsetter
Understanding the Difference Between JavaScript Stack and Heap Memory

Can const-defined values be changed?

Partially can change, partially cannot. const-defined primitive types cannot be altered, but objects defined with const can have their properties modified.

JS heap and stack memory

In a JavaScript engine, variables are stored mainly in two locations: heap memory and stack memory.

Similar to Java, stack memory stores various primitive types—including Boolean, Number, String, Undefined, Null—and also stores pointers to objects. The stack feels like a linear arrangement where each small unit has roughly equal size.

Heap memory is responsible for storing object‑type variables such as Object.

Variables in stack memory generally have a known size or a bounded range, making them simple storage. Data stored in the heap, especially object‑type data, often has an unknown size.

Why const-defined values can be partially changed

When we define a const object, the constant actually refers to a pointer; the pointer to the heap memory is immutable, but the data inside that heap memory—its size or properties—can change. For const‑defined primitive variables, the value itself is immutable, similar to the pointer.

Why const and let variables cannot be redeclared

Each time const or let is used to initialize a variable, the engine first scans the current stack for a name clash; if a duplicate name is found, an error is returned.

Is an instance created with the new keyword stored in stack memory?

When a constructor creates a new instance, the result is an object, not a primitive type.

let str1 = new String('123')
let str2 = new String('123')
console.log(str1==str2, str1===str2) // false false

Clearly, if str1 and str2 were stored in stack memory they would be equal, but the result shows they are not equal, indicating both are stored in heap memory with different pointers.

Value types and reference types essentially refer to stack‑memory variables and heap‑memory variables; concepts such as value passing vs. reference passing, deep copy vs. shallow copy all revolve around stack‑heap memory.

Memory allocation and garbage collection

Generally, stack memory is linearly ordered, has a small capacity, and the system allocates it efficiently. Heap memory first allocates a new storage region, then stores a pointer in the stack, making it relatively less efficient.

Garbage collection: stack‑memory variables are reclaimed as soon as they are no longer used, whereas heap‑memory variables, due to many uncertain references, are reclaimed only after all referencing variables are destroyed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptMemory Managementgarbage collectionconstStack MemoryHeap Memory
Full-Stack Trendsetter
Written by

Full-Stack Trendsetter

Latest articles, video tutorials, and open-source projects on React, Vue, Angular, Ionic, React Native, Node.js, Mini Programs, and other cutting-edge technologies. A community for sharing and discussing full-stack development trends.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.