What is the difference between `shallow copy` and `deep copy` and which methods in JavaScript create each?

JavaScript Professional Medium

JavaScript Professional — Medium

What is the difference between `shallow copy` and `deep copy` and which methods in JavaScript create each?

Key points

  • Shallow copy duplicates top-level properties only
  • Deep copy duplicates all nested objects recursively
  • Shallow copies share references to nested objects
  • Deep copies prevent changes from affecting the original
  • Object.assign() and spread {...} create shallow copies

Ready to go further?

Related questions