Learn JavaScript
Copying Objects

Assign one object to another

In JavaScript, a variable does not store the object itself. It stores the address of that object in the memory. The variable has a “reference” to the object. When we copy the object, it copies the reference to that object.

We can use any one of the variables to change the properties and access them using the other.

In the example given below, both the variables user and copyUser reference the same object. We are using the copyUser variable to change the value of the name property. In the second console.log statement, we get Oliver in the output, even when we are accessing the name property using the user variable.