The difference is that when using the spread you always create a new object:
const a = { name: 'Joe Bloggs' } const b = { ...a, age: 27 }; console.log(a === b)
However, using Object.assign , you can mutate an existing object:
const a = { name: 'Joe Bloggs' } const b = Object.assign(a, { age: 27 }); console.log(a === b)
You can still achieve object propagation behavior with Object.assign by passing the object literal as the first argument:
const a = { name: 'Joe Bloggs' } const b = Object.assign({}, a, { age: 27 }); console.log(a === b)
source share