Primitive and Non-Primitive
What are Data Types?
If you have even a little bit of understanding of code, you should know that there is a variety of different “types” of data.
Certain functions may only be applied to a specific data type.
For example:
At the same time, the different data types are stored differently inside a computer’s memory.
Primitive vs Non-Primitive
Before I go into what the two categories of data types are, I will list which data types belong in which category.
Primitive
Numbers
Strings
Booleans
undefined
null
Non-Primitive (referred to collectively as Objects)
Objects
Arrays
Functions
The two categories represent the two different ways these data types are stored into memory. Primitives are stored by value while Non-Primitives (Objects) are stored by reference.
Let’s look at an example of how Primitives are stored:
What happened here?
We set two different variables: a and b.
a was set to equal 5.
b was set to be equal to a.
Then we changed the value of a to equal 10.
Yet somehow, the value of b was still 5!
This is because Primitives are stored by value.
What this means is that every time we decide to declare a new variable using a primitive data type we are creating a new address in memory for that value.
Let’s look back at our example:
The type of data for a is a Number which is a Primitive data type. It has a value of 5.
The type of data for b is also a Number. It has a value of a.
However, what the variable b is storing is not the variable a itself but the value of the variable.
a has a value of 5, so b is storing 5 into its address.
Later on, even if we decide to change the value of a, this will not affect the value that has already been stored inside b.
This is why when we compare the two now, they are no longer equivalent.
Now let’s look at Non-Primitives (Objects):
We set two different variables: a and b.
a was set equal to an array holding a single value: 10.
b was set equal to a.
Comparing a and b returns true.
Next we modified the data inside a.
Yet, comparing the two variables once again we still return true.
The reason for this is due to Non-Primitives (Objects) are stored by reference.
Look at it this way:
Basically, we are always storing pointers to an address when working with Non-Primitive data types.
Since this is the case, if we modify any variable which points to the same address we will change the value stored inside the address itself.
Let’s look at this another way:
What do you think is the result of the above line of code?
What exactly are we comparing in that line of code?
Are they pointing to the same address?
The answer?
False!
Why is this the case?
While on the surface, they both contain the same value, both are arrays, and both contain a single element of equal value (10).
However, what you’re comparing is not the value but the addresses.
The [10] on the left has a different address in memory than the [10] on the left.
The equality operator (===) is checking if they are pointing to the same address which is why the result is false.
Conclusion
Primitive data types are stored by value. Non-Primitive data types are stored by reference. When declaring a variable, you are generally creating a potential new address.
In the case of storing primitives, that variable is pointing to a new address for that primitive value. When storing a primitive value (even if it’s just a variable that is set to a value), you are storing a value itself into a new address.
In the case of non-primitive objects, the variable is saving a reference to the object. It’s not creating a new address for a value, just a pointer to the object. If you modify a variable pointing to the address, you’re actually modifying the data stored inside the address itself.
Last updated
Was this helpful?