Hello everyone!
Before we move to the topic, let’s explore the following examples and try to understand what’s going on. As we know, constants cannot change through re-assignment and constants cannot be re-declared
Example 1.
The above example will output the value of number as 42
Now we will assign a different value for the same constant.
So now what will be the output? As we know JavaScript is going to throw an error as bellow.
Same as we will consider another example.
Example 2
So the above example will output the value of object as bellow.
Now we will try to modify the value for the same constant object and see what happens.
As like the previous example will JavaScript throw an error? Let’s go throw the answer bellow.
Answer is NO!!!!
Why is that????
Here comes, the concept of Primitive and Non-Primitive of JavaScript.
Primitive values are numbers, strings, Boolean, null & undefined. These Primitive Data Types also are immutable, which means that once created they cannot be modified. If a variable is assigned with a primitive type, we can say that variable is a primitive value. Moreover, Primitive types are passed by value.
Objects like arrays and functions are called as Non-primitive values.
Non-primitive values are mutable data types where the value of an object can be changed later after it gets created/initiated. Objects are not compared by value. They are not strictly equal even if two objects have the same values and properties. Same goes for arrays. They are not strictly equal even if they have the same elements that are in the same order.
Non-primitive values can also be called as reference types because they are being compared by reference instead of value. That means reference points to the memory where an object is located. The variables don’t actually contain the value. Therefore, when we are modifying an object it will not modify the reference. That’s why JavaScript allows us to modify a constant object. But when we try to re-assign the value of the object, then JavaScript will throw the same error as in example 1.
I hope everyone got an idea about Primitive and Non-Primitive types of JavaScript. I have included all the source code to the following GitHub repository.
I hope everyone got an idea about Primitive and Non-Primitive types of JavaScript. I have included all the source code to the following GitHub repository.