Helping you solve those tough coding problems!

Deep clone an object with JavaScript Deep clone an object with JavaScript

Published on Feb 21, 2019 by Jamie Munro

To copy/clone an object using JavaScript, you will want to leverage the JSON.parse function in combination with the JSON.stringify function.




If you are looking for a jQuery solution, you will want to leverage the extend function which can be found in the Deep clone an object with jQuery article.

Using JSON.parse and JSON.stringify to clone



In the following example two objects will be created: objectA and objectB. objectA will be used as the existing object that will be copied/cloned into objectB.



Once objectA has been cloned to objectB using the JSON.parse function in combination with the JSON.stringify function, the output of the copy is outputted on screen and shows that the full object, including child properties, have been copied to the new object.

Tags: JavaScript | clone | JSON.parse

My Books