How to use the spread operator to combine two objects and add new properties in JavaScript

1 Answer

0 votes
const obj1 = { name: 'Tom' };
const obj2 = { age: 51 };
    
const obj = {...obj1, ...obj2, city: 'Houston Texas'}
console.log(obj);

 


/*
run:
       
{ name: 'Tom', age: 51, city: 'Houston Texas' }
  
*/

 



answered Mar 29, 2020 by avibootz
...