BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

ยฉ 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutRSSGitHubX
Posts/javascript
Beginner2 min read

Deep Copy vs Shallow Copy in JavaScript

Understand the difference between shallow and deep copies, with examples and pitfalls.

Mohsen Fallahnejad

Mohsen Fallahnejad

Sep 26, 20252 readsDiscuss

๐Ÿ“ Definitions#

  • Shallow copy
    A new object is created, but its nested objects/arrays are still referenced (not copied).
  • Deep copy
    A new object is created, and all nested objects/arrays are recursively copied, so they are fully independent.

๐Ÿ“ฆ Shallow Copy Examples#

js
const obj = { user: "Ali", settings: { theme: "dark" } };

// Shallow copies
const copy1 = { ...obj };
const copy2 = Object.assign({}, obj);

copy1.user = "Sara";
console.log(obj.user); // "Ali" (independent primitive)

copy1.settings.theme = "light";
console.log(obj.settings.theme); // "light" (nested object was shared!)

๐Ÿ‘‰ Only the top-level properties are copied. Nested references still point to the same objects.


๐Ÿ“ฆ Deep Copy Examples#

Using structuredClone (modern JS)#

js
const obj = { user: "Ali", settings: { theme: "dark" } };
const deepCopy = structuredClone(obj);

deepCopy.settings.theme = "light";
console.log(obj.settings.theme); // "dark" โœ… independent

Using JSON trick (not recommended for all cases)#

js
const deepCopy = JSON.parse(JSON.stringify(obj));

โš ๏ธ This fails with Date, Map, Set, undefined, functions, circular references.

Using libraries#

  • Lodash: _.cloneDeep(obj)
  • Immer: handles immutable updates with deep cloning.

๐Ÿ›‘ Pitfalls of Shallow Copy#

  • Mutating nested structures leaks changes:
    js
    const state = { user: { name: "Ali" } };
    const next = { ...state };
    next.user.name = "Sara";
    
    console.log(state.user.name); // "Sara" โŒ unexpected mutation
    
  • In React/Redux, this can cause stale renders because React thinks the state is unchanged (reference is same).

โœ… When to Use#

  • Shallow copy is fine when:

    • Object has only primitives.
    • You donโ€™t need to mutate nested structures.
    • Performance is critical and you control object shape.
  • Deep copy is needed when:

    • You must ensure no shared references.
    • Working with nested state in frameworks (e.g., Redux).
    • Serializing complex objects (careful with special types).

๐Ÿ” Performance Considerations#

  • Deep copy is slower and heavier because it traverses entire object trees.
  • Shallow copy is cheap, just reassigns top-level references.

โœ… Summary#

  • Shallow copy: Copies top-level properties only; nested objects are shared.
  • Deep copy: Recursively copies everything; no shared references.
  • Use shallow copy for flat/simple objects, and deep copy when you need complete independence.

โœจ Rule of thumb: If you change a nested value and donโ€™t want it to affect the original โ†’ you need a deep copy.

Filed under#javascript#copy#objects#cloning

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X โ†—GitHub โ†—LinkedIn โ†—
โ† OlderWhy setState Doesnโ€™t Cause Hydration ErrorsNewer โ†’Why MUI Avoids Theme Flash on First Load (Dark/Light Mode)

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

The Leaderboard Arrived Sorted. JavaScript Re-sorted It by User ID.

Why JavaScript object key order puts integer-like keys first in ascending order, how that silently reorders JSON from your API, and when to use an array or Map.

Nov 3, 20265 min read

The $4.35 Item That Was Charged $4.34

Why 0.1 + 0.2 !== 0.3 in JavaScript, how floating-point math drops a cent when converting dollars to cents, and why money should be stored as integers.

Nov 3, 20265 min read1

The Settings Object That Came Back undefined

How automatic semicolon insertion (ASI) turns `return` followed by a newline into `return;`, and the lines where JavaScript refuses to add a semicolon at all.

Nov 2, 20265 min read

Get new posts in your inbox

No spam. Unsubscribe any time.

2 min left
Discuss๐Ÿ‘ React
๐Ÿ‘