JavaScript copies by value.
In the first example:
- The
miles
variable is0
. - An object is returned with a
miles
property that has a copy of0
in it. - The
miles
variable is updated (now50
) . - The
miles
property is read (still0
).
In the second example:
- The
miles
variable is 0. - An object is returned with a
miles
property containing a function that returns the value of themiles
variable. - The
miles
variable is updated (now50
). - The
miles
function is called and returns the value of themiles
variable (50
).