xxxxxxxxxx
// es6
const obj = {name: 'john', surname: 'smith'};
const objCopy = {obj};
xxxxxxxxxx
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
xxxxxxxxxx
// 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
xxxxxxxxxx
const originalObject = { name: "John", age: 25 };
const copyObject = { originalObject };
xxxxxxxxxx
var arr1 = [1, 2, 3]
var arr2 = [arr1]
arr2.push(4)
console.log(arr1) // [1, 2, 3]
console.log(arr2) // [1, 2, 3, 4]
// Or
var obj1 = { a: 1 }
var obj2 = {obj1}
obj2.a = 2
console.log(obj1) // { a: 1 }
console.log(obj2) // { a: 2 }
// ... spreads the array/object