Types of memory in JavaScript

  1. Stackmemory :- It stores primitive data type. Size is known as compile time. It stores static data. Allocated a fixed amount of memory.

Let's understand by a simple code :

"code"

"output"

Here, if num2 is equal to num1 then when we update the value of num2, num1 value should also be changed. But this didn't happen because in stack memory we are given copy of data not exact one. So when we declare num2 = num1 internally num2 gets copy of num1 , num1 gets unchanged when we change num2.

  1. Heapmemory :- It stores non-primitive data type i.e arrays and objects. Size is known as runtime. It stores dynamic data. There is no limit to the amount of memory.

"code2"

"output2"

Here, in this case when we declare that obj2 is equal to obj1, we didn't get copy rather than, we got refrence to the same data present in heap memory. So, when we change obj2 the data present in obj1 is also gets changed.