merge object in javascript

using spread operator

Table of contents

No heading

No headings in the article.

merge object using spread operator

summary : in this tutorial , you will learn how to merge two or more object and create new object that combine all properties of all objects.

ES6 introduce spread oprator(...) which can be used to merge two or more object and create new object that has properties of merge object.

in following example uses the (spread operator )to merge person and job objects

into employee object :


let person = {
    firstName: 'fuzail',
    lastName: 'khan',
    age: 23,
};
let job = {
    jobTitle: 'javascript developer',
    location: 'wfh',
};

let employee ={
    ...person, ...job
};
console.log(employee)

output:=>
 {
  firstName: 'fuzail',
  lastName: 'khan',
  age: 23,
  jobTitle: 'javascript developer',
  location: 'wfh'
}