JavaScript object deep cloning: 3 ways

Deep cloning in JavaScript

Spread operator ...

Use the JavaScript spread operator like so

const source = { b: 4, c: 5 };
const target = { ...source };
console.log(target);

Results: > Object { b: 4, c: 5 }

Object.assign

const source = { b: 4, c: 5 };
const target = Object.assign({}, source);
console.log(target);

Results: > Object { b: 4, c: 5 }

JSON serialization

const source = { b: 4, c: 5 };
const target = JSON.parse(JSON.stringify(source));
console.log(target);

Results: > Object { b: 4, c: 5 }