Blog/NotesConcept

Shallow Copy and Deep Copy in JavaScript - Object Cloning

Understand Shallow copy and Deep copy in JavaScript with detailed examples, and what is the difference between shallow copy vs deep copy in JavaScript.

Intermediate

Anuj Sharma

Last Updated Feb 21, 2026


 Shallow Copy and Deep Copy in JavaScript - Object Cloning

When working with objects and arrays in JavaScript, you’ll often need to copy or clone data but not all copies are same, thats where understanding Shallow Copy and Deep Copy in JavaScript helps.

In frontend interviews, this is a common topic because understanding how data is referenced in memory helps you debug unexpected behaviour in your web application.
In this post, Let’s learns Shallow copy and Deep copy in detail.

Table of Contents

  1. Basic of Shallow Copy and Deep Copy
  2. Understand Object Shallow Copy in JavaScript
  3. Understand Object Deep Copy in JavaScript
  4. Shallow Copy vs Deep Copy
  5. Learn more

Basics of Shallow Copy and Deep Copy

In simple words:

  • A shallow copy copies only the first level of the object and links the reference of nested levels.
  • A deep copy copies everything, including nested objects at all the levels.

So, if the original object changes inside its nested levels, a shallow copy might still get affected but a deep copy stays completely independent.

Understand Object Shallow Copy in JavaScript with Example

A shallow copy means that you copy the top-level properties, but if any property is an object, it’s still linked to the original object

This happens because JavaScript objects are reference types, and it means they store memory addresses, not the actual value.

Example

Here is the quick example showing how shallow copy referencing to the nested original object.

const user = {
  name: "Anuj",
  address: {
    city: "Delhi",
    country: "India",
  },
};

// Create a shallow copy using spread
const shallowCopy = { ...user };

// Modify the nested object
shallowCopy.address.city = "Mumbai";

// Shallow copy still referencing to the original address
// That's why changes reflected to the original object.
console.log(user.address.city); 
// Output: "Mumbai"

Even though we changed shallowCopy.address.city, the original user object also changed.

That’s because the address object was copied by reference, not as a new object. 

Common ways to make a shallow copy:

1. Using the spread operator (...)

const shallowCopy = { ...user };

2. Using Object.assign()

Object.assign(shallowCopy, user);

Understand Object Deep Copy in JavaScript with Example

A deep copy creates a completely new copy of the object and its nested properties. Changes in the deep-copied object do not affect the original.

This is useful when you want to safely modify data without altering the original, such as managing React state, Redux data, or API responses.

Example

const user = {
  name: "Anuj",
  address: {
    city: "Delhi",
    country: "India",
  },
};

// Deep copy using JSON methods, Not the most optimal way
const deepCopy = JSON.parse(JSON.stringify(user));

// Modify the nested object
deepCopy.address.city = "Mumbai";

console.log(user.address.city); // Output: "Delhi"
console.log(deepCopy.address.city); // Output: "Mumbai"

This time, changing the nested property in deepCopy didn’t affect the original user object.

Common ways to make a shallow copy:

1. (Optimal Way) Using structuredClone() 

const deepCopy = structuredClone(user);

2. Using JSON.stringify()

const deepCopy = JSON.parse(JSON.stringify(user));

Shallow Copy vs Deep Copy

Let’s break down the differences one by one — this will help you clearly understand how both work and when to use which.

1. Definition

  • Shallow Copy: Copies only the top-level properties of an object.
  • Deep Copy: Copies everything, including all nested objects and arrays.

2. Nested Objects Handling

  • Shallow Copy: The nested objects are still linked (by reference) to the original object. So, changing nested data in the copied object will also update it in the original one.
  • Deep Copy: The nested objects are completely separated and independent. Changes made in one do not affect the other.

3. Memory Usage

  • Shallow Copy: Uses less memory because it reuses references for nested objects.
  • Deep Copy: Takes more memory since it creates new copies of all nested structures.

4. Performance

  • Shallow Copy: Faster to create since it only copies one level.
  • Deep Copy: Slightly slower because it needs to recursively copy every nested property.

5. When to Use

  • Shallow Copy: Best for small or simple objects where nested changes don’t matter.
  • Deep Copy: Ideal for complex data or when you want to protect the original data (like in React state updates).

6. Common Methods to implement

  • Shallow Copy:
    • Using the spread operator - { ...obj }
    • Using Object.assign()
  • Deep Copy:
    • Using JSON.parse(JSON.stringify(obj))
    • Using structuredClone()

Learn Next

  1. Machine Coding Cheat Sheet
  2. Frontend System Design Cheat Sheet
  3. Promise polyfill in JavaScript
  4. Promise.all polyfill in JavaScript
  5. Best Polyfill resources to ace JavaScript Interview

🚀

Love this content? Share it!

Help others discover this resource

About the Author

Anuj Sharma

A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me  🚀

Comments

Be the first to share your thoughts!

Guest User

Please login to comment

0 characters


No comments yet.

Start the conversation!

Share Your Expertise & Help the Community!

Build Your Portfolio

Help the Community

Strengthen Your Skills

Share your knowledge by writing a blog or quick notes. Your contribution can help thousands of frontend developers ace their interviews and grow their careers! 🚀


Other Related Blogs

Implement useFetch() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Feb 21, 2026

Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.

setTimeout Polyfill in JavaScript - Detailed Explanation

Anuj Sharma

Last Updated Aug 3, 2025

Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.

Implement useSessionStorage() Custom Hook in React [Interview]

Anuj Sharma

Last Updated Nov 15, 2025

Understand the code implementation of useSessionStorage custom hook in react that will help to efficiently manager session storage in application.

Top 10 React Performance Optimization Techniques [React Interview]

Anuj Sharma

Last Updated Feb 21, 2026

Find the top React Performance Optimization Techniques specific to React applications that help to make your react app faster and more responsive for the users along with some bonus techniques.

Implement useToggle() Custom Hook in React (Interview)

Anuj Sharma

Last Updated Feb 21, 2026

Explore code explanation of useToggle() custom hook in react to handle the toggle event efficiently.

21 Most Asked Frontend System Design Interview Questions & Patterns

Anuj Sharma

Last Updated Dec 17, 2025

A comprehensive collection of the most asked Frontend System Design Interview Questions for Experienced along with the related Answers, Patterns and Important Resources.

Stay Updated

Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

All in One Preparation Hub to Ace Frontend Interviews. Master JavaScript, React, System Design, and more with curated resources.

Consider Supporting this Free Platform

Buy Me a Coffee

Product

HomeFrontend InterviewInterview ExperienceBlogsToolsLeaderboard

Tools

CSS Image FilterPixelate ImageAspect Ratio CalculatorBox Shadow GeneratorCSS Gradient GeneratorNeumorphism GeneratorExplore More Tools →

© 2026 FrontendGeek. All rights reserved