What is the difference between shallow and deep copy?
Answer
A shallow copy creates a new object with the same top-level properties as the original, but nested objects/arrays are still shared references. Changes to nested objects affect both the original and the copy. Methods that create shallow copies: spread operator ({...obj}, [...arr]), Object.assign({}, obj), Array.from(arr), arr.slice(), arr.concat(). A deep copy recursively copies all nested objects and arrays — the copy is completely independent. Methods: structuredClone(obj) (native, ES2022, handles most types including Date, Map, Set, circular references), JSON.parse(JSON.stringify(obj)) (works for JSON-safe data only — loses functions, undefined, Date objects, circular references), Lodash _.cloneDeep(obj). Rule of thumb: for primitive values, copying is always safe. For nested mutable data, use deep copy when you need full independence. React state patterns require immutable updates — shallow copy outer object, replace nested references.