Last active
September 25, 2022 05:51
-
-
Save Sachin-chaurasiya/30f97a282141e7ff80ce2b8cbed1c42d to your computer and use it in GitHub Desktop.
JavaScript Rest 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 one object i.e developer. | |
const developer={ | |
name:"Sachin Chaurasiya", | |
age:22, | |
githubHandle:"https://github.com/Sachin-chaurasiya", | |
portfolio:"https://sachinchaurasiya.dev", | |
blog:"https://blog.sachinchaurasiya.dev" | |
} | |
// Use case : we want to use this developer object but without key "githubHandle". | |
// there are two solutions | |
// 1. delete githubHandle property using delete keyword | |
delete developer.githubHandle | |
console.log(developer) | |
/* { | |
name:"Sachin Chaurasiya", | |
age:22 | |
portfolio:"https://sachinchaurasiya.dev", | |
blog:"https://blog.sachinchaurasiya.dev" | |
} | |
*/ | |
// 2. use rest operator | |
const {githubHandle, ...newDeveloper}=developer | |
console.log(newDeveloper) | |
/* { | |
name:"Sachin Chaurasiya", | |
age:22 | |
portfolio:"https://sachinchaurasiya.dev", | |
blog:"https://blog.sachinchaurasiya.dev" | |
} | |
*/ | |
/* | |
In first solution we have deleted the "githubHandle" property from developer object, | |
we can't access the "githubHandle" property any more. | |
In second solution we use rest operator to create new copy of developer object without "githubHandle" property, | |
and still we can access the "githubHandle" property. | |
so always try to use rest operator instead of delete keyword. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment