Enjoy 20% off all plans by following us on social media. Check out other promotions!

Deep Clone

Senior Engineer, Ex-Amazon

The term "deep clone" is not formally defined in JavaScript's language specification, but is generally well understood in the community. A deep clone makes a copy of JavaScript value, leading to a completely new value that has no references pointing back to the properties in the original object (if it's an object). Any changes made to the deep-copied object will not affect the original object.

Implement a deepClone function that performs a deep clone operation on JavaScript objects. You can assume the input only contains JSON-serializable values (null, boolean, number, string, Array, Object) and will not contain any other objects like Date, Regex, Map or Set.


const obj1 = { user: { role: 'admin' } };
const clonedObj1 = deepClone(obj1);
clonedObj1.user.role = 'guest'; // Change the cloned user's role to 'guest'.
clonedObj1.user.role; // 'guest'
obj1.user.role; // Should still be 'admin'.
const obj2 = { foo: [{ bar: 'baz' }] };
const clonedObj2 = deepClone(obj2);
obj2.foo[0].bar = 'bax'; // Modify the original object.
obj2.foo[0].bar; // 'bax'
clonedObj2.foo[0].bar; // Should still be 'baz'.


Premium FeaturePurchase premium to see companies which ask this question.
View plans