JavaScript Objects Object rest/spread (...)

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

7

Object spreading is just syntactic sugar for Object.assign({}, obj1, ..., objn);

It is done with the ... operator:

let obj = { a: 1 };

let obj2 = { ...obj, b: 2, c: 3 };

console.log(obj2); // { a: 1, b: 2, c: 3 };

As Object.assign it does shallow merging, not deep merging.

let obj3 = { ...obj, b: { c: 2 } };

console.log(obj3); // { a: 1, b: { c: 2 } };

NOTE: This specification is currently in stage 3



Got any JavaScript Question?