How to merge two objects in JavaScript

Published on

Thanks to the introduction of the spread operator we can easily merge two objects into one in Javascript. Using the following method:

const firstObject = {
  make: 'Land Rover',
  model: 'Discovery',
};

const secondObject = {
  color: 'Black',
};

const mergedObject = { ...firstObject, ...secondObject };

Be careful when merging objects that share attribute names as the second will overwrite the first.