In many languages, the developer can choose between assigning/passing a value as the value itself, or as a reference to the value. In JS, however, this decision is entirely determined by the kind of value. That surprises a lot of developers from other languages when they start using JS.
If you assign/pass a value itself, the value is copied. For example:
Here’s how you can prove there’s two separate values involved:
See how yourName
wasn’t affected by the re-assignment of to "Frank"
? That’s because each variable holds its own copy of the value.
Consider:
Because the value assigned to myAddress
is an object, it’s held/assigned by reference, and thus the assignment to the yourAddress
variable is a copy of the reference, not the object value itself. That’s why the updated value assigned to the is reflected when we access yourAddress.street
. myAddress
and yourAddress
have copies of the reference to the single shared object, so an update to one is an update to both.