All programming languages have their in-built data structures, however, they differ from each other.
The values of these types are classified into Primitive Values and Non-primitive/Reference Values. The primitive values are values of the data types that includes String, Number, Boolean, Null, and Undefined while the Non-primitive or reference values are values of the data types Functions, Array, and Object.
Primitive Data Type
Primitive data types are immutable i.e they can not be modified.
Let us check out this example to understand better.
let foo = 20 // Initial assignment let bar = foo // Assignment of variable *bar* to *foo* variable foo = 40 // Re-assignment console.log(foo) // 40 console.log(bar) // 20
From this code, we can see that the variable
foo was initially assigned the value of 20 and that variable is pushed to the top of the stack. A new variable
bar was assigned to
foo, in the case of a primitive data type of which the type
number is inclusive, only the value of
foo which is 20 is assigned to
bar. The variable
bar with its value is pushed to the call stack.
In the next line, we can see a reassignment of the value 40 to the variable
foo. When we log both variables to the console, we can see the results of both variables.
From the code above, we can understand that the reassignment of a value to a primitive data type does not change the value of the initial assignment.
From the image above, we can see that for each assignment of the variables, they are placed at the top of the stack.
Reference Data Type
Reference data types are mutable i.e they can be easily modified.
In reference type, the assignment of a variable is done by reference. All the reference data types are technically objects so we would collectively refer to them as Objects.
The variables that are assigned to a non-primitive value are usually given a reference to that value. This reference points to that object's address/location in memory. Note that the variables do not themselves contain the value.
In the code above, we created an object in memory, and a location and an address of the array are created. Using memory allocation, the address is the stack while the location is the heap. All variables usually first point to the stack. If it is a non-primitive value, the stack would contain a reference to the object in the heap.
From the image above, we can see that for the assignment of the
obj variable, the value is not associated with the variable rather an address/reference is pushed to the stack, and a location/object is pushed to the heap. For the assignment of the
newObj variable to the
obj, it points to the same address and location of the
obj variable. When there is a reassignment of the
new Obj property, this affects the location thereby changing the value of every variable that points to that location, in our case, affecting the
The memory of the heap is not usually ordered which is why we need to keep a reference to it in the stack.
We can think of the REFERENCES in the stack as the ADDRESS and the OBJECTS in the heap as the LOCATION that the address belongs to.
In this article, we were able to:
Understand that primitive data types are immutable i.e they can not be modified, and a non-primitive data type can be easily modified.
Some resources that helped me understand this concept better include:
If you found this article helpful, don't forget to give a thumbs-up 👍 , and please share with your network. I would appreciate a comment if you have any questions or feedback.