🧠 Day 11 of 21days challenge Shallow vs Deep Copy in JavaScript ⚠️ const copy = { ...obj }; Looks like a copy… but not always safe. Shallow copy duplicates only top-level properties. Deep copy creates a completely independent object. For easy understanding :- Shallow copy = shared nested reference Deep copy = fully independent copy Shallow changes affect original 👉 That’s why bugs happen in state updates This changed how I handle object mutations 🚀 #JavaScript #Frontend #InterviewPrep
a very easy way is to use structuredClone a new and easiest way for deepCloning hope you will explore that..
One of the very common interview questions that is nearly always ask in interview, now the morden way to deeply clone the nested object method is structuredClone provided by JS itself , very well explained!
Don't use JSON stringify/parse to clone. There is https://developer.mozilla.org/en-US/docs/Web/API/Window/structuredClone for many years now.