To merge two objects in JavaScript, you can use the spread operator(...). It creates a new object with all the properties of the first and second objects. If there are two properties with the same name, it is overwritten by the property from the second object.

const obj1 = { a: 1, b: 1, c: 1 }
const obj2 = { b: 2, c: 2, d: 4, e: 5 }

const obj = { ...obj1, ...obj2 }
// { a: 1, b: 2, c: 2, d: 4, e: 5 }

If you don’t want to create a new object and just looking to merge the second object with the first object, you should use the Object.assign() method instead. This method takes in one or more source objects and merges them into the target object.

const job = { title: 'iOS Developer', salary: '76k USD', location: 'Remote' }
const changes = { salary: '99k USD' }

Object.assign(job, changes)

console.log(job)
// { title: 'iOS Developer', salary: '99k USD', location: 'Remote' }