Best way to copy an object in JavaScript?


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?


Did you find this post useful? Show some love!

I use spread to copy and assign a new value to the old object

const copy=old=>{
  return {...old};

const add=(old,val)=>{
  return {...old,...val};

but this is the same as Object.assign({}, obj);, is not a deep copy.

In plain javascript there are only one truth.

JSON.parse( JSON.stringify( obj ) );

For me, in some cases, i don't know how and why, but Object.assign is working, but it's not recommended for use without deep understanding of it.

This is great for simple object literals. But, if someone out there tries it and it doesn't work, allow me to offer some reasons why.

  • JSON.parse will fail for any object that recurses on itself. Try this in your console: var a = {}; a.a = a; JSON.stringify(a);. This is uncommon in app-level code, but happens a lot in DOM parsing libraries and frameworks.
  • This method doesn't allow you to make "shallow copies" (copies of the immediate properties of the object, without going any deeper). jQuery and Lodash, on the other hand, have options that allow you to do this.
  • Objects that have a toJSON() method will not truly be copied as-is. The toJSON() method will be invoked and its response will be assumed to be correct JSON, whether or not it corresponds to the actual properties of the object.
  • Methods will not be copied.
  • Dates will be parsed as Strings, not as Dates.

To cover your own butt, you're better off doing as others have suggested and using jQuery or Lodash. In modern ES6 code, you don't have to worry about code bloat, since you can just import the method you want to use from either toolkit and the rest of the library won't get included in your browser bundle.

JSON.parse(JSON.stringify(obj)) doesn't work for things that don't get stringified:

> JSON.parse(JSON.stringify({ a: () => 1, b: 2, c: null, d: undefined }))
{ b: 2, c: null }

If you don't want to use a library or module for this, I think a (with a hasOwnProperty) check is probably the best bet.

Wouldn't 'object.c === undefined' be true for both the first and the second, meaning that in pretty much every sense of the word, the two are indeed equal?

Sort of, but depending on how you check for what's in an object it wouldn't be:

> const a = {}
> const b = { thing: undefined }
> 'thing' in a
> 'thing' in b
> a.hasOwnProperty('thing')
> b.hasOwnProperty('thing')
> Object.keys(a).length
> Object.keys(b).length



Hey there, we see you aren't signed in. (Yes you, the reader. This is a fake comment.)

Please consider creating an account on It literally takes a few seconds and we'd appreciate the support so much. ❤️

Plus, no fake comments when you're signed in. 🙃

Well I suppose it depends on how deep the objects are and whether you have control over the organization of the objects. I believe for simple scenarios, you could do something along the following. Dunno if you qualify ES6 as Vanilla JS, so here's some ES5:

function Garage(x, y, cars) {
  // dimensions of the garage in feet, because America :/
  this.area = {
    width: x,
    depth: y,
  } = cars;

  this.copy = function(garage){
    this.area.width = garage.area.width;
    this.area.depth = garage.area.depth;
    for (var i = 0; i <; i++) {[i] = new Car([i].make,[i].model);

function Car(make, model) {
  this.make = make;
  this.model = model;

  var someGarage = new Garage(40.0, 25.0, [
    new Car("Toyota", "Camry"),
    new Car("Toyota", "Prius"),
    new Car("Tesla", "Model 3"),
  var otherGarage = new Garage(25.0, 25.0, [
    new Car("Toyota", "Camry"),

  // make the second garage a copy of the first, without using references

  // change the first garage to show that the garages aren't related[2] = null;

  console.log("someGarage: ", someGarage);
  console.log("otherGarage: ", otherGarage);

The output to the console:

someGarage:  {}
    area: Object { width: 41, depth: 25 }
    cars: []
        0: Object { make: "Toyota", model: "Camry" }
        1: Object { make: "Toyota", model: "Prius" }
        2: null
        length: 3
        __proto__: Array []
    copy: function Garage/this.copy()
    __proto__: Object {  }

otherGarage:  {}
    area: Object { width: 40, depth: 25 }
    cars: []
        0: Object { make: "Toyota", model: "Camry" }
        1: Object { make: "Toyota", model: "Prius" }
        2: Object { make: "Tesla", model: "Model 3" }
        length: 3
        __proto__: Array []
    copy: function Garage/this.copy()
    __proto__: Object {  }

Alternatively, if you have to handle dynamic objects of unknown dimensions, you could use a recursive function that uses Object.getOwnPropertyNames and the prototypes (someObject.__proto__) of those properties to handle the generation of fresh Objects for the properties of the destination object.

Hope that answers your question!

Edit: Added console output for the above script.

That does indeed look like a good solution if you know the object props.

But JSON.parse( JSON.stringify( obj ) );, though hacky, is much quicker to write!

Oh okay. I misunderstood, I thought you were avoiding using that JSON.parse( JSON.stringify( obj ) ); for some reason. Whoops!

Have a good Turkey day, or a good Thursday.

I'm all in for vanilla JS but to a point, I would suggest copy the function from Lodash or jQuery. Use the code that was written and tested over the years, circular references can be a pain in the ..code.

I would simply use recursion for a deep copy:

function deepCopy(obj) {
 if(typeof obj === 'object') {
  return Object.keys(obj)
   .map(k => ({ [k]: deepCopy(obj[k]) }))
   .reduce((a, c) => Object.assign(a, c), {});
 } else if(Array.isArray(obj)) {
 return obj;

Good ol' var copy = {}; for (var item in obj) { obj.hasOwnProperty(item) && (copy[item] = obj[item]); } approach works most of the times.

About those cases when this doesn't work: maybe you're solving the wrong problem.

Good to see I'm not crazy! JSON.parse( JSON.stringify( obj ) ); seems to be a common 'shortcut'. But assigning each prop to a new object with obj.hasOwnProperty(item) also looks like a good option if you know which properties to look for.

So you're saying Object.assign({}, obj); doesn't work?

It does work, but not for nested objects. i.e.:

const x = {
  foo: {
    bar: false
const y = Object.assign({}, x);
console.log(x === y); // outputs false
console.log( ===; // outputs true

Yeah it doesn't work on nested objects.

I would use lodash and its assignIn method. I haven't tried yet Object.assign() so I don't know if it copies nested object and references.

What happens to cycles? For example

let a = {};
a.a = a;

Is the assumption that only data objects with no functions or cycles are being copied?

Normally I would use lodash's clone() or cloneDeep() to do that, if I have it into the project already, otherwise the good old JSON.parse( JSON.stringify( obj ) ); is always a great option.

Classic DEV Post from May 24

Share Your Best Motivational Quotes

Share Your Best Motivational Quotes

Follow @saigowthamr to see more of their posts in your feed.
Peter Tasker
Programmer in Ottawa Canada.
More from @ptasker
Using async await with jQuery’s $.ajax
#code #javascript #jquery
JavaScript’s cryptic ‘this’ – what, when and why
#code #javascript
Trending on
Narcissistic number
#challenge #javascript #python #etc
Back to basics: Naming
#naming #javascript #programming #discuss
Intro to the GraphQL
#graphql #javascript #webdev #beginners
Check if the chain has the same amount
#challenge #javascript #python #etc
Dynamic Programming vs Divide-and-Conquer
#javascript #algorithms #webdev #computerscience
How to create a Hacker News API GraphQL data source for GatsbyJS
#gatsby #webdev #javascript
Using Fetch API to Get and Post
#javascript #es6fetch #api ~ From KnockoutJS to VueJS
#curriculumcoke #vue #knockoutjs #javascript