var x = {myProp: "value"};
var y = Object.assign({}, x);
// es6
const obj = {name: 'john', surname: 'smith'};
const objCopy = {...obj};
var x = {key: 'value'}
var y = JSON.parse(JSON.stringify(x))
//this method actually creates a reference-free version of the object, unlike the other methods
//If you do not use Dates, functions, undefined, regExp or Infinity within your object
// NORMAL COPY---------------------------------------
const a = { x: 0}
const b = a;
b.x = 1; // also updates a.x
// SHALLOW COPY---------------------------------------
const a = { x: 0, y: { z: 0 } };
const b = {...a}; // or const b = Object.assign({}, a);
b.x = 1; // doesn't update a.x
b.y.z = 1; // also updates a.y.z
// DEEP COPY---------------------------------------
const a = { x: 0, y: { z: 0 } };
const b = JSON.parse(JSON.stringify(a));
b.y.z = 1; // doesn't update a.y.z
let man1 = {
name:"codepadding",
age:29
}
let man2 = {...man1};
const clone = structuredClone(object);