Deep copy vs Shallow copy in JavaScript

Asked by Lisa Park Apr 16, 2025 intermediate 1051 views
61

I need to copy an object in JavaScript, but changes to the copy affect the original object. What's happening?

const original = { name: 'John', address: { city: 'NYC' } };
const copy = { ...original };
copy.address.city = 'LA';
console.log(original.address.city); // 'LA' - Why?!

How do I make a true deep copy?

Solutions

0 answers

No solutions yet

Be the first to help solve this problem!