JavaScriptInterview preparation
Intermediate·Concept·2 min read

Is spread a deep copy?

Practice this question

Definition:

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.

Key points:

  • No. { ...user } copies only the first level.

  • Nested objects still share references.

  • Copy each changed level, or use structuredClone for supported data types.

  • Changing a top-level field in the copy does not change that field in the original.

  • structuredClone supports many nested data types, but not functions and not every custom object behavior.

Example:

JavaScript
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 address object.

Interview point:

  • 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?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.