HTML and CSS Reference
In-Depth Information
//
An immutable object is an object that cannot be changed after it is first created.
Even when these appear to change, as in the examples below, in reality a new
object is being created.
In order to see immutability in action, declare a new variable called
t
, and assign it the
value of
s
:
> t = s
"Hello world"
We can now print out the value of
t
, and also confirm that
t
and
s
are equal:
> t
"Hello world"
> s == t
true
What should happen if we now modify the string held against the variable
s
? We can ap-
pend to the string value using the
+=
operator:
> s += 'test'
"Hello worldtest"
If you now print out the value of
s
you will see that the string's value appears to have
changed:
> s
"Hello worldtest"
Despite this, if you print out the value of
t
it has retained its old value: