Register to get access to free programming courses with interactive exercises

Cloning (copying) JS: Objects

Object cloning is another common operation in development, especially frontend. Cloning creates a copy of the original object, i.e., a new object filled with the same data.

JavaScript has no built-in function to perform cloning, but you can emulate it using Object.assign(). To do this, just pass an empty object as the first parameter, and the one you want to clone as the second parameter:

const user = { name: 'Tirion', email: 'support@hexlet.io', age: 44 };

// user properties are copied to the newly created object
const copyOfUser = Object.assign({}, user);

user === copyOfUser; // false

The result is two different objects with the same content. Since they are different, changes in one do not change the data in the other.

Cloning is also performed using the clone() function from the lodash library. Although its result is identical to the examples above, it has better operation semantics due to its name.

import _ from 'lodash';

const user = { name: 'Tirion', email: 'support@hexlet.io', age: 44 };
const copyOfUser = _.clone(user);

Cloning with the methods above does not affect nested objects. They end up in the new object from the reference stored in the old one.

const user = { company: { name: 'Hexlet' } };
const copyOfUser = Object.assign({}, user);
// This is the same object
user.company === copyOfUser.company; // true

user.company.createdAt = 2012;
console.log(copyOfUser.company.createdAt); // 2012

This cloning is called shallow cloning. It's very important to remember that this is what JavaScript means when it uses the term "cloning". This is not a bad thing, shallow cloning is handy for many tasks. But if isn't enough, you have to use deep cloning instead.

There is no built-in way in JavaScript to clone objects completely. Therefore, it's customary for developers to use the ready-made cloneDeep() function from the lodash library.

import _ from 'lodash';

const user = { company: { name: 'Hexlet' } };
const copyOfUser = _.cloneDeep(user);

user.company === copyOfUser.company; // false

Deep cloning has one serious disadvantage. If the object is large and has a complex structure deep cloning can have a big impact on performance, and this is why it's not a default method.

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

Bookmate
Health Samurai
Dualboot
ABBYY