Skip to content

Commit f37220e

Browse files
committed
complete rest pattern and parameters
1 parent 0749313 commit f37220e

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,96 @@
11
# Array Destructuring
22

33
This is an ES6 feature that allows you break a complex data structure like array into a smaller data structure like the array.
4+
5+
# Spread Operator
6+
7+
The spread operator (...) is used to quickly copy all or part of an existing array or object into another array or object.
8+
9+
The spread operator is a bit similar to destructuring; it allows you handle complex arrays and break it down into smaller arrays. t
10+
11+
Difference between the spread operator and destructuring is that spread operators does not create new variables. It can only be used in places where values are seperated by commas.
12+
13+
For instance, you can't use it in a template literal like the example below. This is because the literal expects a single value and not an array of values.
14+
15+
```js
16+
const data = [1, 2, 3, 4];
17+
const [a, b, c, d] = [...data];
18+
console.log(a, b, c, d);
19+
console.log(...data);
20+
```
21+
22+
Another use case of the spread operator is for passing in multiple values into a function.
23+
24+
# Rest Patter and Parameters
25+
26+
The rest pattern is the opposite of the spread operator. It uses the exact same syntax, however, it it used to collect multiple elements and condense them into an array, whereas, the spread operator is used for unpacking an array into multiple elements or array.
27+
28+
### 1. Rest: Destructuring
29+
30+
Here's an example of the spread operator below. We know we're working with the spread syntax because it is declared on the right side of the assignment operator `=`.
31+
32+
```js
33+
// SPREAD: Because [...] is on the right side of assignment operator =
34+
const arr = [1, 2, ...[3, 4]];
35+
```
36+
37+
We can also use it on the left side along with destructuring. For Example:
38+
39+
```js
40+
// REST: Because [...] is on the left side of assignment operator =
41+
const [a, b, ...others] = [1, 2, 3, 4, 5];
42+
console.log(a, b, others);
43+
```
44+
45+
The output will be `1 2 [3 4 5]` because 1 and 2 will be stored in the a and b variables and then the rest of the value will be stored together in a new array.
46+
47+
This is where the name; Rest is coined from. It collects the elements that are unused in a destructuring assignment.
48+
49+
We can also have the `[...]` dots on both side of the operator.
50+
51+
```js
52+
const [pizza, , risotto, ...otherFood] = [
53+
...restaurant.mainMenu,
54+
...restaurant.starterMenu,
55+
];
56+
57+
console.log(pizza, risotto, otherFood);
58+
```
59+
60+
Here we stored the value of Pizza and Risotto into two new variables `pizza, risotto` and then the rest of the `(...restaurant.mainMenu and ...restaurant.starterMenu)` array was stored into a new array called `otherFood`.
61+
62+
> **NOTE**
63+
> It's good to remember that the rest pattern should always be the last element in a destructuring assignment otherwise JavaScript will not know what till when it should collect the rest of an array.
64+
65+
For example: You Cannot Do This!
66+
67+
```js
68+
const [pizza, , risotto, ...otherFood, bread]
69+
```
70+
71+
The rest pattern will have to be the last element in other for the operation to work. As a result of this, there can only be one rest pattern in any destructuring assignment.
72+
73+
### Rest in Objects
74+
75+
Just like the spread operator, the rest operator also works on objects.
76+
77+
### 1. Rest: Functions
78+
79+
Just like the spread operator that allows passing in multiple values in a function all at once, the rest operator can do the opposite.
80+
81+
```js
82+
const add = function (...numbers) {
83+
console.log(numbers);
84+
};
85+
86+
add(2, 3);
87+
add(5, 3, 7, 2);
88+
add(8, 2, 5, 3, 2, 1, 4);
89+
```
90+
91+
### Finally!
92+
93+
The subtle distinction that tells you when and where to use spread and rest.
94+
95+
- Use Spread when you want to write values seperated by commas.
96+
- Use Rest when you want to write variable names seperated by commas.

09-Data-Structure-Modern-Operators-and-Strings/script.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ const restaurant = {
9494
orderPasta: function (ing1, ing2, ing3) {
9595
console.log(`Here is yout declicious pasta with ${ing1}, ${ing2}, ${ing3}`);
9696
},
97+
98+
orderPizza: function (mainIngredient, ...otherIngredients) {
99+
console.log(mainIngredient, otherIngredients);
100+
},
97101
};
98102

99103
// Calling orderDelivery method with new object parameters
@@ -161,7 +165,7 @@ const newMenu = [...restaurant.mainMenu, "Gnocci"];
161165
console.log(newMenu);
162166

163167
/*
164-
? Difference btw spread operator and destructuring is that spread operators does not create new variables. It can only be used in places where values are seperated by commas.
168+
? Difference btw spread operator and destructuring is that spread operators does not create new variables. It can only be used in places where values are seperated by commas.
165169
*/
166170

167171
// Copy arrays
@@ -177,7 +181,7 @@ const str = "Eke";
177181
const letters = [...str, "", "S"];
178182
console.log(letters);
179183

180-
// Spread operators can only be used when building an array or when values are passed into a funciton.
184+
//? Spread operators can only be used when building an array or when values are passed into a funciton.
181185
// console.log(${...str} 'Victor'); Will not work
182186

183187
// Real-world example
@@ -199,3 +203,40 @@ console.log(newRestaurant);
199203
const restaurantCopy = { ...restaurant };
200204
restaurantCopy.name = "Ristorante Roma";
201205
console.log(restaurantCopy.name, restaurant.name);
206+
207+
// 🔸Rest Pattern and Parameters🔸
208+
const data = [1, 2, ...[3, 4]];
209+
210+
// Rest: Destructuring
211+
const [j, k, ...others] = [1, 2, 3, 4, 5];
212+
console.log(j, k, others);
213+
214+
const [pizza, , risotto, ...otherFood] = [
215+
...restaurant.mainMenu,
216+
...restaurant.starterMenu,
217+
];
218+
219+
console.log(pizza, risotto, otherFood);
220+
221+
const { sat, ...weekdays } = { ...restaurant.openingHours };
222+
console.log(sat, weekdays);
223+
224+
// Rest: Functions
225+
const add = function (...numbers) {
226+
console.log(numbers);
227+
let sum = 0;
228+
for (let i = 0; i < numbers.length; i++) {
229+
sum += numbers[i];
230+
}
231+
console.log(sum);
232+
};
233+
234+
add(2, 3);
235+
add(5, 3, 7, 2);
236+
add(8, 2, 5, 3, 2, 1, 4);
237+
238+
const x = [23, 5, 7];
239+
add(...x);
240+
241+
restaurant.orderPizza("Mushrooms", "Onions", "Olives", "Spinach");
242+
restaurant.orderPizza("Mushrooms");

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<title>JavaScript Course</title>
1010
</head>
1111
<body>
12-
<h1>Console.log()</h1>
12+
<h1>Console.log</h1>
1313
<script src="09-Data-Structure-Modern-Operators-and-Strings/script.js"></script>
1414
</body>
1515
</html>

0 commit comments

Comments
 (0)