Learn JavaScript
Copying Objects

Cloning Object Properties

We can create a clone of an object by iterating over all the properties of the object.

In the example given below, we are iterating over the properties of the user object using a for...in loop. Then we are copying the properties of user into userClone object.

Here, changing the name property using the userClone object does not change the name property of the user object.