Understand Shallow copy and Deep copy in JavaScript with detailed examples, and what is the difference between shallow copy vs deep copy in JavaScript.
Anuj Sharma
Last Updated Oct 5, 2025
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.
In simple words:
So, if the original object changes inside its nested levels, a shallow copy might still get affected but a deep copy stays completely independent.
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.
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.
1. Using the spread operator (...)
const shallowCopy = { ...user };
2. Using Object.assign()
Object.assign(shallowCopy, user);
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.
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.
1. (Optimal Way) Using structuredClone()
const deepCopy = structuredClone(user);
2. Using JSON.stringify()
const deepCopy = JSON.parse(JSON.stringify(user));
Let’s break down the differences one by one — this will help you clearly understand how both work and when to use which.
{ ...obj }Object.assign()JSON.parse(JSON.stringify(obj))structuredClone()Be the first to share your thoughts!
No comments yet.
Start the conversation!
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! 🚀
Anuj Sharma
Last Updated Nov 10, 2025
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.
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Nov 11, 2025
Understand step by step how to flatten nested array in javascript using recursion, also explore the flatten of complex array of object.
Anuj Sharma
Last Updated Nov 10, 2025
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.
Anuj Sharma
Last Updated Oct 2, 2025
Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.
Anuj Sharma
Last Updated Nov 16, 2025
Implement useThrottle Custom Hook In React (Interview) to limit the number of APi calls to improve the performance of application.
Subscribe to FrontendGeek Hub for frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2025 FrontendGeek. All rights reserved