spread syntax with objects

In JavaScript, the spread syntax (...) can be used in object literals to copy the properties from one object into another object. It provides a concise way to merge multiple objects or clone an object.

Here’s an example of using spread syntax with objects:

const obj1 = { a: 1, b: 2 };

const obj2 = { c: 3, d: 4 };

// Merging two objects using spread syntax

const mergedObj = { …obj1, …obj2 };

console.log(mergedObj); // Output: { a: 1, b: 2, c: 3, d: 4 }

// Cloning an object using spread syntax

const clonedObj = { …obj1 };

console.log(clonedObj); // Output: { a: 1, b: 2 }

Leave a comment

Your email address will not be published. Required fields are marked *