Back to Developer Roadmap

Spread Operator Objects

src/data/question-groups/javascript/content/spread-operator-objects.md

4.0537 B
Original Source

The spread operator (...) allows you to merge objects through a function call or directly combine objects. Below is an example using a function call:

javascript
function mergeObj(firstObj, secondObj) {
        return {...firstObj, ...secondObj} // merge both objects
}

let firstObj = { name: "cess", city: "Lagos"};

let secondObj = {occupation: "developer", countriesVisited: 2};

let newObj = mergeObj(firstObj, secondObj);

console.log(newObj); // {name: 'cess', city: 'Lagos', occupation: 'developer', countriesVisited: 2}