Object Assign( ) Method to Copy or Merge Objects
Object.assign(target, ...sources) copies the properties of each source object into the target object, and then returns the target. It is used to merge several objects into one, and to make a copy of an object by assigning it into a new empty one.
Merging objects
Section titled “Merging objects”Sources are copied from left to right, so where two sources have a property with the same name, the later one wins. That makes it a natural way to apply a user’s choices on top of a set of defaults:
const defaults = { currency: 'GBP', giftWrap: false, delivery: 'standard' };const choices = { giftWrap: true, delivery: 'next day' };
const order = Object.assign({}, defaults, choices);console.log(order);console.log(defaults);This prints:
{ currency: 'GBP', giftWrap: true, delivery: 'next day' }{ currency: 'GBP', giftWrap: false, delivery: 'standard' }The target here is a new empty object, so defaults is left unchanged.
The target is changed
Section titled “The target is changed”Object.assign() does not build a new object of its own. It writes into the target and returns that same object, so passing an existing object as the target changes it:
const basket = { items: 2 };const result = Object.assign(basket, { total: 18.5 });
console.log(basket);console.log(result === basket);This prints:
{ items: 2, total: 18.5 }truePass {} as the first argument whenever the original objects should stay as they are.
The copy is shallow
Section titled “The copy is shallow”Only the top level of properties is copied. Where a property holds an object or an array, the copy gets a reference to the same object or array, so a change made through one is seen through the other:
const list = { name: 'Weekend', tasks: ['shop', 'garden'] };const copy = Object.assign({}, list);
copy.name = 'Sunday';copy.tasks.push('iron');
console.log(list);console.log(copy);This prints:
{ name: 'Weekend', tasks: [ 'shop', 'garden', 'iron' ] }{ name: 'Sunday', tasks: [ 'shop', 'garden', 'iron' ] }Changing name on the copy left the original alone, because a string is copied by value, but both objects share one tasks array. To copy every level, use structuredClone(), which is built into current browsers and Node:
const list = { name: 'Weekend', tasks: ['shop', 'garden'] };const deep = structuredClone(list);
deep.tasks.push('iron');console.log(list.tasks, deep.tasks);This prints:
[ 'shop', 'garden' ] [ 'shop', 'garden', 'iron' ]Deep vs Shallow Copy goes into the difference in more detail.
Spread syntax
Section titled “Spread syntax”In current code, spread syntax inside an object literal does the same merge into a new object and is usually easier to read:
const defaults = { currency: 'GBP', giftWrap: false, delivery: 'standard' };const choices = { giftWrap: true, delivery: 'next day' };
const order = { ...defaults, ...choices, note: 'Leave with neighbour' };console.log(order);This prints:
{ currency: 'GBP', giftWrap: true, delivery: 'next day', note: 'Leave with neighbour'}Both forms copy only the source’s own enumerable properties, so inherited properties and those marked as not enumerable are left out. The one real difference is that Object.assign() sets each property on the target, which runs any setter the target defines, whereas spread always defines plain new properties on a new object. Object.assign() remains the choice when the aim is to change an existing object in place.
Steve Griffith covers this topic in his video Using Object assign( ) to Copy or Merge Objects.