JavaScript clone object

How to do a shallow or deep clone of a JavaScript Object using JS ES5, JS ES6 or Lodash. Please note that in the shallow copy the nested properties are just copied by reference.

JavaScript ES5

JSON.stringify() Deep Clone

Be aware that you can't use JSON.stringify() to clone Functions and that Date objects will be stringified in the process.

var clone = JSON.parse(JSON.stringify(obj));

JavaScript ES6

Object.assign() Shallow Clone FASTEST

const clone = Object.assign({}, obj);

spread operator Shallow Clone

const clone = {...obj};


cloneDeep() Deep Clone

var clone = _.cloneDeep(obj, true);

clone() Shallow Clone

var clone = _.clone(obj, true);