Is spread a deep copy?
Try code in the playgroundUnderstand the concept
Spread syntax creates a new outer object or array by copying its enumerable contents. It is a shallow copy, so nested objects are still shared references. This matters when updating React state or copying request data that another part of the application may modify.
Important ideas
-
No.
{ ...user }copies only the first level. -
Nested objects still share references.
-
Copy each changed level, or use
structuredClonefor supported data types. -
Changing a top-level field in the copy does not change that field in the original.
-
structuredClonesupports many nested data types, but not functions and not every custom object behavior.
Work through the example
const original = { address: { city: 'Surat' } };
const copy = { ...original };
copy.address.city = 'Ahmedabad';
console.log(original.address.city); // Ahmedabad- Both outer objects still point to the same nested
addressobject.
Can you explain it without the notes?
Is spread a deep copy?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Spread syntax creates a new outer object or array by copying its enumerable contents. It is a shallow copy, so nested objects are still shared references. This matters when updating React state or copying request data that another part of the application may modify.
Turn this concept into a clear answer.
- Show the nested reference that remains shared; do not describe spread as complete immutability.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “Is spread a deep copy?”. Examples are study fragments; framework behavior can depend on your version.