Best way to copy an object in JavaScript?

ptasker

Peter Tasker

Posted on November 22, 2017

Best way to copy an object in JavaScript?

Object.assign()

So I'm always looking for a way to use vanilla JS whenever possible these days, and I discovered that deep copying an object in JavaScript is still weird.

StackOverflow reminded me of the JSON.parse( JSON.stringify( obj ) ) trick, and it looks like Object.assign still doesn't copy nested objects.

jQuery's $.extend() works. But that's not vanilla JS any more.

What hacks do you guys use for copying JS objects?

¯\_(ツ)_/¯

💖 💪 🙅 🚩
ptasker
Peter Tasker

Posted on November 22, 2017

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related