Skip to content

Instantly share code, notes, and snippets.

@Sachin-chaurasiya
Last active January 16, 2022 11:20
Show Gist options
  • Save Sachin-chaurasiya/f4aa781a315eab9759ebe5f8ba074159 to your computer and use it in GitHub Desktop.
Save Sachin-chaurasiya/f4aa781a315eab9759ebe5f8ba074159 to your computer and use it in GitHub Desktop.
JavaScript Spread Operator
// 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