Java-Script Rest and Spread Operators:
In JavaScript, we are using three dots ( ... ) for both REST and SPREAD Operators. But these two operators are not the same they both have a different purpose.
REST Operator
- Purpose of this operator is to collect all remaining elements into the array.
- For Example:
- let take one array in below example
- O/P :
Necklace
[ 'Earring', 'Payal', 'Brooch', 'Bangles']
- In the Above example three-dot contain the rest of the product time in the restProdcut variable that calls Destructuring that breaks our code into small pieces.
- so in the above example, mainProduct variable contains my Main product and restProduct varibale contains my rest products.
- The rest operator always gives rest data into array format only.
- while creating function we can also use rest operator like below
SPREAD Operator:
- It's work opposite to rest operators, like in rest operator we are collecting item into an array but in spread operator we are unpack the collected items into a single elements.
- For Examples
- O/P : [ 'Necklace', 'Earring', 'Payal', 'Brooch', 'Bangles'];
- We can say SPREAD operator is used for copy one array into another array or concatenates.
I hope you guys enjoyed this tutorial 😼
Comments
Post a Comment