Advertisement
728x90
You can use the Object.assign() method which is used to copy the values and properties from one or more source objects to a target object. It returns the target object which has properties and values copied from the source objects. The syntax would be as below,
javascript
1Object.assign(target, ...sources);Let's take example with one source and one target object,
javascript
1const target = { a: 1, b: 2 };
2
3const source = { b: 3, c: 4 };
4
5const returnedTarget = Object.assign(target, source);
6
7console.log(target); // { a: 1, b: 3, c: 4 }
8
9console.log(returnedTarget); // { a: 1, b: 3, c: 4 }As observed in the above code, there is a common property(b) from source to target so it's value has been overwritten.
Advertisement
Responsive Ad
🎯 Practice NowRelated Challenge
JavaScript Coding Exercise 22
Test your knowledge with this interactive coding challenge.
Start CodingAdvertisement
728x90
193of476