Shallow & Deep Copy
Shallow Copy
Deep Copy

Difference between Shallow and Deep copy

shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.



1. Cloning using object spread
2. Cloning using object rest
3. Cloning using Object.assign()


there is a method Object.assign() which is used to copy the values and properties from one or more source objects to a target object.
It returns the target object which has properties and values copied
from the target object. Object.assign() does not throw on null or undefined source values.
Object.assign() is used for cloning an object.
Object.assign() is used to merge object with same properties.


const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget); // expected output: Object { a: 1, b: 4, c: 5 }