Let us take an example:
const arr = ["a", "b", "c"]
arr will be assigned an address to an object created in the heap, which has keys 0, 1, 2 i.e indexes as a key.
As arrays are nothing but an object in js.
When you do shift operation on const
arr, we are not changing the value of address assigned to variable arr
in stack memory, we are just removing 0th key from the object at that address.
Stack memory Heap Memory
| | | { |
| | | 0 : "a" |
| | | 1 : "b" |
| | | 2 : "c" |
|arr = 8k| | } |
---------- -------------
Here, 8k is the address of the object created in heap memory.
Which is assigned to variable arr in stack memory.
So, after doing a shift
operation our arr
changes to ["b", "c"]
Stack memory Heap Memory
| | | { |
| | | |
| | | 1 : "b" |
| | | 2 : "c" |
|arr = 8k| | } |
---------- -------------
0th key from object in heap memory is removed.