This is a simple example which I hope explains clearly:
Start with two variables and a constant.
var foo = [10,11];
var bar = const [20,21];
const baz = [30,31];
Try to modify foo
and it succeeds.
foo.add(12); // [10,11,12]
Try to similarly modify bar
and there's an error, because even though bar
is a variable, its value was declared to be a constant, thus is immutable.
bar.add(22); // ERROR!
Try to reassign a different value to bar
. That works since bar
itself was not declared as a constant.
bar = [40,41];
Now, try to modify bar
's value again and this time it works, since its new value is not a constant.
bar.add(42) // [40,41,42]
Try to modify baz
and there's an error, since baz
being declared as a constant itself, its value is inherently immutable.
baz.add(32); // ERROR!
Try reassigning baz
to a new value and it fails because baz
is a constant and can't be reassigned to a new value.
baz = [50,51]; // ERROR!