Object cloning in JavaScript is creating a copy of an object that doesn’t reference the original object in memory. There are several cloning methods, each with its own features: shallow or deep copying, support for different data types, and performance.
Main cloning methods:
Object cloning is an important concept in JavaScript that allows creating independent copies of data. The correct method choice depends on copying depth requirements and data types in the object.
Copies only enumerable properties of the first level:
const original = { a: 1, b: { c: 2 } };
const copy = Object.assign({}, original);Modern syntax for shallow copying:
const copy = { ...original };Simple deep copying method with limitations:
const deepCopy = JSON.parse(JSON.stringify(original));Full deep copying of all data types:
function deepClone(obj) {
// Recursive copying implementation
}Modern browser API for deep copying:
const deepCopy = structuredClone(original);const user = {
name: 'John',
address: {
city: 'Moscow',
street: 'Lenina'
}
};
// Shallow copying
const shallow = { ...user };
shallow.address.city = 'Saint Petersburg';
// Original object will also change!
// Deep copying
const deep = structuredClone(user);
deep.address.city = 'Kazan';
// Original object won't changeconst data = {
items: [1, 2, { value: 3 }]
};
// Different methods give different results
const assignCopy = Object.assign({}, data);
const spreadCopy = { ...data };
const jsonCopy = JSON.parse(JSON.stringify(data));// ❌ Using spread for deep copying
const config = {
database: {
host: 'localhost',
port: 5432
}
};
const copy = { ...config };
copy.database.host = 'remote'; // Will change original too!
// ✅ Using structuredClone for deep copying
const deepCopy = structuredClone(config);// ❌ JSON methods don't work with functions, undefined, Symbol
const obj = {
func: () => {},
undef: undefined,
sym: Symbol('test'),
date: new Date()
};
const copy = JSON.parse(JSON.stringify(obj));
// Functions, undefined and Symbol will be lost!
// ✅ Using structuredClone
const properCopy = structuredClone(obj);The correct cloning method choice depends on data structures, performance requirements, and supported browsers.
What will be the result and why?
const original = {
name: 'Test',
data: {
value: 42,
nested: {
flag: true
}
},
tags: ['js', 'clone'],
func: function() { return this.name; }
};
// Method 1
const copy1 = { ...original };
// Method 2
const copy2 = JSON.parse(JSON.stringify(original));
// Method 3
const copy3 = structuredClone(original);
// What will happen when changing?
copy1.data.value = 100;
copy2.tags[0] = 'javascript';
copy3.data.nested.flag = false;
console.log(original.data.value);
console.log(original.tags[0]);
console.log(original.data.nested.flag);
console.log(copy2.func);Answer:
100
js
true
undefined
Explanation:
copy1 (spread operator):
copy1.data remains a reference to original.datacopy1.data.value affects original.data.valueoriginal.data.value becomes 100copy2 (JSON methods):
copy2.tags — independent array copycopy2.tags[0] doesn’t affect original.tags[0]original.tags[0] remains ‘js’copy2.func will be undefined, as functions are lost during JSON serializationcopy3 (structuredClone):
copy3.data.nested — independent copycopy3.data.nested.flag doesn’t affect original.data.nested.flagoriginal.data.nested.flag remains true❗ Error in analysis above — let’s reconsider:
Actually:
original.data.value will be 100 (changed via copy1)original.tags[0] will remain ‘js’ (copy2.tags is independent copy)original.data.nested.flag will remain true (copy3.data.nested is independent copy)copy2.func will be undefined (functions are lost in JSON)Correct result:
100
js
true
undefined
This example demonstrates important differences between cloning methods:
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪