How to merge to two objects in JavaScript?


I have two objects,

const p = {
   name: "Ajay",
   age: 20,
};

const q = {
   branch: "CSE",
};

I want to merge these two to a single object like below, how to do it?

1 Answer

4 years ago by

const r = {...p, ...q};

console.log(r);

Ouput,

{ name: 'Ajay', age: 20, branch: 'CSE' }

4 years ago by Divya