What is the difference between shallow and deep copying of an object?

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#JavaScript #Objects #JS Basics

Brief Answer

Shallow copying copies only the top level properties of an object. If properties contain other objects, only references to them are copied. Deep copying copies all nesting levels, creating a completely independent copy.


Full Answer

The difference between shallow and deep copying is one of the important topics in JavaScript. Understanding this difference helps avoid many mistakes.

Shallow Copying

Copies only the first level properties:

const original = { 
  name: 'John', 
  address: { city: 'Moscow' } 
};
 
const shallow = { ...original };
// shallow.name — copy
// shallow.address — reference to the same object!

Deep Copying

Copies all nesting levels:

const original = { 
  name: 'John', 
  address: { city: 'Moscow' } 
};
 
const deep = structuredClone(original);
// deep.name — copy
// deep.address — also copy, new object

Main Difference

Shallow Copying

const user = { 
  name: 'John', 
  address: { city: 'Moscow' } 
};
 
const shallow = { ...user };
shallow.address.city = 'Piter';
console.log(user.address.city); // 'Piter' — original object changed!

Deep Copying

const user = { 
  name: 'John', 
  address: { city: 'Moscow' } 
};
 
const deep = structuredClone(user);
deep.address.city = 'Piter';
console.log(user.address.city); // 'Moscow' — original object not changed

Simple Examples

When Deep Copying Matters

// Working with forms
const defaultForm = {
  user: { name: '', age: 0 },
  settings: { theme: 'light' }
};
 
// Shallow copy — dangerous!
const form1 = { ...defaultForm };
form1.user.name = 'John';
// Now defaultForm.user.name is also 'John'!
 
// Deep copy — safe
const form2 = structuredClone(defaultForm);
form2.user.name = 'Peter';
// defaultForm unchanged

Copying Methods

Shallow

// Spread operator
const shallow1 = { ...original };
 
// Object.assign
const shallow2 = Object.assign({}, original);

Deep

// structuredClone (modern way)
const deep1 = structuredClone(original);
 
// JSON methods (with limitations)
const deep2 = JSON.parse(JSON.stringify(original));

Important Points

1. Performance

// Shallow — fast
const shallow = { ...obj };
 
// Deep — slower, especially for large objects
const deep = structuredClone(obj);

2. Type Support

// JSON methods lose functions, undefined, Symbol
const obj = { func: () => {}, undef: undefined };
const copy = JSON.parse(JSON.stringify(obj));
// copy.func and copy.undef will disappear!
 
// structuredClone preserves all types
const copy2 = structuredClone(obj);
// All data preserved

Common Mistakes

1. Wrong Method Choice

// ❌ Using shallow for nested objects
const config = { database: { host: 'localhost' } };
const copy = { ...config };
copy.database.host = 'remote';
console.log(config.database.host); // 'remote' — unexpected!
 
// ✅ Using deep copying
const copy2 = structuredClone(config);
copy2.database.host = 'remote';
console.log(config.database.host); // 'localhost' — correct

2. Using JSON Methods with Functions

// ❌ JSON methods don't work with functions
const obj = { method: () => {} };
const copy = JSON.parse(JSON.stringify(obj));
// copy.method will disappear!
 
// ✅ Using structuredClone
const copy2 = structuredClone(obj);
// All data preserved

Simple Rules

  1. Shallow — for simple objects without nested references
  2. Deep — for complex structures with nested objects
  3. structuredClone — modern and reliable deep copying method
  4. JSON methods — fast, but with limitations
  5. Careful with performance — deep copying is slower

Understanding the difference between shallow and deep copying helps write predictable code and avoid reference errors.


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪