3 Ways to Clone Objects in JavaScript

3 Ways to Clone Objects in JavaScript

Because objects in JavaScript are references values, you can't simply just copy using the =. But no worries, here are 3 ways for you to clone an object

Because objects in #JavaScript are references values, you can't simply just copy using the =. But no worries, here are 3 ways for you to clone an object πŸ‘

const food = { beef: 'πŸ₯©', bacon: 'πŸ₯“' }

// "Spread"

{ ...food }

// "Object.assign"

Object.assign({}, food)

// "JSON"



// { beef: 'πŸ₯©', bacon: 'πŸ₯“' }

Objects are Reference Types

Your first question might be, whey can't I use =. Let's see what happens if we do that:

const obj = {one: 1, two: 2};

const obj2 = obj;

  obj,  // {one: 1, two: 2};
  obj2  // {one: 1, two: 2};

So far, both object seems to output the same thing. So no problem, right. But let's see what happens if we edit our second object:

const obj2.three = 3;

// {one: 1, two: 2, three: 3}; <-- βœ…

// {one: 1, two: 2, three: 3}; <-- 😱

WTH?! I changed obj2 but why was obj also affected. That's because Objects are reference types. So when you use =, it copied the pointer to the memory space it occupies. Reference types don't hold values, they are a pointer to the value in memory.

If you want to learn more about this, check out Gordon's Zhu Watch and Code course. It's free to enroll and watch the video "Comparison with objects". He gives a super awesome explanation on it.

1. Using Spread

Using spread will clone your object. Note this will be a shallow copy. As of this post, the spread operator for cloning objects is in Stage 4. So it's not officially in the specifications yet. So if you were to use this, you would need to compile it with Babel (or something similar).

const food = { beef: 'πŸ₯©', bacon: 'πŸ₯“' };

const cloneFood = { ...food };

// { beef: 'πŸ₯©', bacon: 'πŸ₯“' }

2. Using Object.assign

Alternatively, Object.assign is in the official released and also create a shallow copy of the object.

const food = { beef: 'πŸ₯©', bacon: 'πŸ₯“' };

const cloneFood = Object.assign({}, food);

// { beef: 'πŸ₯©', bacon: 'πŸ₯“' }

3. Using JSON

This final way will give you a deep copy. Now I will mention, this is a quick and dirty way of deep cloning an object. For a more robust solution, I would recommend using something like lodash

const food = { beef: 'πŸ₯©', bacon: 'πŸ₯“' };

const cloneFood = JSON.parse(JSON.stringify(food))

// { beef: 'πŸ₯©', bacon: 'πŸ₯“' }

Lodash DeepClone vs JSON

Here's a comment from the community. Yes, it was for my previous post, How to Deep Clone an Array. But the idea still applies to objects.

Alfredo Salzillo: I'd like you to note that there are some differences between deepClone and JSON.stringify/parse.

  • JSON.stringify/parse only work with Number and String and Object literal without function or Symbol properties.
  • deepClone work with all types, function and Symbol are copied by reference.

Here's an example:

const lodashClonedeep = require("lodash.clonedeep");

const arrOfFunction = [() => 2, {
    test: () => 3,
}, Symbol('4')];

// deepClone copy by refence function and Symbol
// JSON replace function with null and function in object with undefined

// function and symbol are copied by reference in deepClone
console.log(lodashClonedeep(arrOfFunction)[0] === lodashClonedeep(arrOfFunction)[0]);
console.log(lodashClonedeep(arrOfFunction)[2] === lodashClonedeep(arrOfFunction)[2]);

@OlegVaraksin: The JSON method has troubles with circular dependencies. Furthermore, the order of properties in the cloned object may be different.

Shallow Clone vs Deep Clone

When I used spread ... to copy an object, I'm only creating a shallow copy. If the array is nested or multi-dimensional, it won't work. Here's our example we will be using:

const nestedObject = {
  country: 'πŸ‡¨πŸ‡¦',
    city: 'vancouver'

Shallow Copy

Let's clone our object using spread:

const shallowClone = { ...nestedObject };

// Changed our cloned object
clonedNestedObject.country = 'πŸ‡ΉπŸ‡Ό'
clonedNestedObject.country.city = 'taipei';

So we changed our cloned object by changing the city. Let's see the output.

// {country: 'πŸ‡ΉπŸ‡Ό', {city: 'taipei'}} <-- βœ…

// {country: 'πŸ‡¨πŸ‡¦', {city: 'taipei'}} <-- 😱

A shallow copy means the first level is copied, deeper levels are referenced.

Deep Copy

Let's take the same example but applying a deep copy using "JSON"

const deepClone = JSON.parse(JSON.stringify(nestedObject));

// {country: 'πŸ‡ΉπŸ‡Ό', {city: 'taipei'}} <-- βœ…

// {country: 'πŸ‡¨πŸ‡¦', {city: 'vancouver'}} <-- βœ…

As you can see, the deep copy is a true copy for nested objects. Often time shallow copy is good enough, you don't really need a deep copy. It's like a nail gun vs a hammer. Most of the time the hammer is perfectly fine. Using a nail gun for some small arts and craft is often case an overkill, a hammer is just fine. It's all about using the right tool for the right job πŸ€“


Unfortunately, I can't write a test for spread because it's not officially in the spec yet. Nevertheless, I included it in the test so you can run it in the future 😝. But the result shows Object.assign is a lot faster than JSON.

Performance Test

Community Input

Object.assign vs Spread

@d9el: It's important to note that Object.assign is a function which modifies and returns the target object. In Samantha's example using the following,

const cloneFood = Object.assign({}, food) 

{} is the object that is modified. The target object is not referenced by any variable at that point, but because Object.assign returns the target object, we are able to store the resulting assigned object into the cloneFood variable. We could switch our example up and use the following:

const food = { beef: '🌽', bacon: 'πŸ₯“' };

Object.assign(food, { beef: 'πŸ₯©' });

// { beef: 'πŸ₯©', bacon: 'πŸ₯“' }

Obviously, the value of beef in our food object is wrong, so we can assign the correct value of beef using Object.assign. We aren't actually using the returned value of the function at all, but we are modifying our target object which we have referenced with the const food.

Spread on the other hand is an operator which copies properties of one object into a new object. If we wanted to replicate the above example using spread to modify our variable food...

const food = { beef: '🌽', bacon: 'πŸ₯“' };

food = {
  beef: 'πŸ₯©',
// TypeError: invalid assignment to const `food'

... we get an error, because we use spread when creating new objects, and therefore are assigning a whole new object to food which was declared with const, which is illegal. So we can either choose to declare a new variable to hold our new object in, like the following:

const food = { beef: '🌽', bacon: 'πŸ₯“' };

const newFood = {
  beef: 'πŸ₯©',

// { beef: 'πŸ₯©', bacon: 'πŸ₯“' }

or we could declare food with let or var which would allow us to assign a whole new object:

let food = { beef: '🌽', bacon: 'πŸ₯“' };

food = {
  beef: 'πŸ₯©',

// { beef: 'πŸ₯©', bacon: 'πŸ₯“' }

Thanks: @d9el

Deep Clone using External Libraries

  • @lesjeuxdebebel: Personally I use jquery with $.extend(); function
  • @edlinkiii: underscore.js ~~ _.clone()
  • @Percy_Burton: The only way I've known to do this is with the Lodash library, cloneDeep method.

    More Ways using JavaScript

  • @hariharan_d3v: Object.fromEntries(Object.entries(food)) [shallow] clones the object.


Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

JavaScript Memory Management System

The main goal of this article is help to readers to understand that how memory management system performs in JavaScript. I will use a shorthand such as GC which means Garbage Collection. When the browsers use Javascript, they need any memory location to store objects, functions, and all other things. Let’s deep in dive that how things going to work in GC.

Grokking Call(), Apply() and Bind() Methods in JavaScript

In this article, we will have a look at the call(), apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function.

JavaScript Tutorial: if-else Statement in JavaScript

This JavaScript tutorial is a step by step guide on JavaScript If Else Statements. Learn how to use If Else in javascript and also JavaScript If Else Statements. if-else Statement in JavaScript. JavaScript's conditional statements: if; if-else; nested-if; if-else-if. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.

Top 15 Free JavaScript Frameworks for Web Applications

List of some useful JavaScript Frameworks and libraries for website, web apps, and mobile apps development, that developers should know about to make selection easier.