DEV Community

Vasil Vasilev
Vasil Vasilev

Posted on

1

Mutable And Immutable Values in Javascript

Image description

In JavaScript there are two types of data types - primitive types and objects. Whenever you define a variable, the JavaScript engine allocates a memory to it. It stores all variable names on the Stack, while the values are either on the Stack or on the Heap.

let str = "hello"; 
// str (variable name) is stored on the Stack
// 'hello' (value) is stored on the Stack

let obj = { fname: "Vasil", lname: "Vasilev"} 
// obj (variable name) is stored on the Stack
// {...} the value is stored on the Heap
Enter fullscreen mode Exit fullscreen mode

NB: The { fname: "Vasil", lname: "Vasilev"} is merely a reference which points to the actual value stored on the Heap.

Any data type value which is immutable is stored on a Stack data structure since it’s size is known during compilation phase.

Mutable data types such as Objects, Arrays are stored on a Heap data structure and a reference to the Object or array is stored on the stack data structure.

$150K MiniMax AI Agent Challenge — Build Smarter, Remix Bolder, Win Bigger!

Join the MiniMax AI Agent Challenge — Build your first AI Agent 🤖

Developers, innovators, and AI tinkerers, build your AI Agent and win $150,000 in cash. 💰

Read more →

Top comments (0)

Heroku

Tired of jumping between terminals, dashboards, and code?

Check out this demo showcasing how tools like Cursor can connect to Heroku through the MCP, letting you trigger actions like deployments, scaling, or provisioning—all without leaving your editor.

Learn More

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay