Consider the following example:
In the example given above,
we are mutating the user object
by changing the value of
the name property.
Note that,
even though we declared
the user variable using
the const keyword,
we can mutate the object.
However, we cannot reassign
a variable declared
using the const keyword.
It is possible to both
mutate and reassign
variables declared using
the let keyword:
In the example above,
-
We first declare the user variable
using the let keyword
and
assign it an object -
{ name: "Sam", age: 32 };.
-
We then reassign user
with another object -
{ name: "Oliver", age: 24 };.
This is possible because the user
variable was declared using let.
-
The bird variable was declared
using the const keyword
and
an object was assigned to it -
{ name: "Parrot", canFly: true };.
-
We then reassign bird
with another object -
{ name: "Penguin", canFly: false };.
This will show an error because
we cannot reassign a variable
declared using the const keyword.
The same concepts apply to arrays
and
other composite data types.