Open In App

How does memory stacks work in Javascript ?

Introduction:

 The memory stack is a mechanism in JavaScript that allows us to allocate and free memory. 



If a programmer wants to store some data, they must first create an empty part of the heap that is called a “stack.” Then, they can push and then pop values on the stack. When working with strings in JavaScript, we typically use string concatenation as one way of pushing data onto the stack (without having to manage memory management manually). 

Note that code like this will not work with arrays and objects because those features do not use the word “stack” anywhere in their process (in order for them to be used effectively, one would have to understand how their internal structuring alters this concept).



Working of memory stack in Javascript: 

Declaring a variable: Consider the following snippet.

var name = “James”; 
var age = 35;

The above variable declaration creates two new variables and stores their respective values. 

This can be visualized as shown below:
When we declare a variable using the var keyword, its name gets associated with an empty part of the heap which is known as a variable object.

Applications of memory stack work in Javascript:

Important points: 

Conclusion: 

Memory stack in javascript is an important programming tool. Memory stack can be visualized as a stack of objects on which we can push and pop values. JavaScript execution engine does not actually follow a stack-based approach to function calls at runtime.

Article Tags :