Primitive vs Reference Values š§µ
ā In JavaScript there are two types of values:
- Primitive values are (null, undefined, boolean, string, number, symbol, BigInt)
- Reference value is (object)
ā Stack and Heap Memory
JS stores Primitive values in the Stack memory bcoz they have fixed memory whereas it stores Reference values in the Heap memory.
In the above image, JS gives stack memory to age, name, and person and links the person variable with the object in the heap.
Therefore person variable is a reference that refers to the objects.
ā You can add or delete properties from the Reference values but Primitives values do not have properties.
ā Copying Values
When you assign a primitive value from one variable to another. JS engine creates a copy of that value and assigns it to the new variable in the Stack memory.
But when you assign a reference value from one variable to another JS engine creates a reference to the same object in the Heap memory.