Hi and welcome to this video course on JavaScript.
Let's take a closer look at common
misunderstandings about objects and arrays.
It's important to understand the concept
of primitive and reference types in JavaScript.
As a reminder, objects and arrays are reference types.
This means that even if you store an array in a constant,
you can still edit the array without breaking the constant restriction.
To demonstrate this
let's take the following example.
Let's create an array that's a constant -hobbies
and let's write reading and cooking inside this array.
Now let's say we want to push a new
element to the existing array using the push method.
So let's do hobbies push programming.
Now if we console log hobbies, the output will be reading,
cooking and programming and there will be no error about editing a constant value.
This is because reference types only store
an address pointing to the memory location where the array is stored.
When we add a new element, this address remains unchanged
and the constant still points to the same location in memory.
It's important to understand this so
that you don't get confused when editing a constant value.
We're not actually editing the constant value, but the value it's pointing to.
So same happens with the object.
So let's create hobbies object
and let's say that this is Nata's hobby and type is programming.
If we want to add to hobbies object
a value of age for example, and we output the hobbies object,
we're going to see age appear here because objects are also reference types.
However, if you create number for example,
and USA is twelve, which is a primitive type,
and now you want to reassign number to 13 and we console log number.
If we run this, we're going to get an error saying
that we're trying to assign something else to our constant variables.
So that's all for this video.
Thanks for watching and see you in the next one.