Last active
January 16, 2022 11:20
-
-
Save Sachin-chaurasiya/f4aa781a315eab9759ebe5f8ba074159 to your computer and use it in GitHub Desktop.
JavaScript Spread Operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Let's say we have two objects i.e userPersonal and userSocial | |
// We want to merge these two objects and send the new object to the server. | |
const userPersonal = { | |
firstName:"Sachin", | |
lastName:"Chaurasiya", | |
email:"sachinchaurasiyachotey87@gmail.com", | |
} | |
const userSocial={ | |
linkedin:"https://www.linkedin.com/in/sachin-chaurasiya", | |
github:"https://github.com/Sachin-chaurasiya", | |
twitter:"https://twitter.com/sachindotcom", | |
portfolio:"https://sachinchaurasiya.dev", | |
blog:"https://blog.sachinchaurasiya.dev" | |
} | |
// we can merge objects in two ways | |
// 1. using Object.assign | |
// 2. using spread operator | |
// using Object.assign | |
/* | |
Object.assign will mutate the original object. | |
*/ | |
const userDetails=Object.assign(userPersonal, userSocial) | |
// using spread operator | |
/* | |
spread operator will not mutate the original object | |
*/ | |
const userDetails={...userPersonal,...userSocial} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment