You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/02-rest-parameters-spread/article.md
+62-18Lines changed: 62 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Rest parameters and spread operator
1
+
# Rest parameters and spread syntax
2
2
3
3
Many JavaScript built-in functions support an arbitrary number of arguments.
4
4
@@ -8,7 +8,7 @@ For instance:
8
8
-`Object.assign(dest, src1, ..., srcN)` -- copies properties from `src1..N` into `dest`.
9
9
- ...and so on.
10
10
11
-
In this chapter we'll learn how to do the same. And, more importantly, how to feel comfortable working with such functions and arrays.
11
+
In this chapter we'll learn how to do the same. And also, how to pass arrays to such functions as parameters.
12
12
13
13
## Rest parameters `...`
14
14
@@ -25,7 +25,7 @@ alert( sum(1, 2, 3, 4, 5) );
25
25
26
26
There will be no error because of "excessive" arguments. But of course in the result only the first two will be counted.
27
27
28
-
The rest parameters can be mentioned in a function definition with three dots `...`. They literally mean "gather the remaining parameters into an array".
28
+
The rest of the parameters can be included in the function definition by using three dots `...` followed by the name of the array that will contain them. The dots literally mean "gather the remaining parameters into an array".
29
29
30
30
For instance, to gather all arguments into array `args`:
31
31
@@ -96,9 +96,7 @@ showName("Julius", "Caesar");
96
96
showName("Ilya");
97
97
```
98
98
99
-
In old times, rest parameters did not exist in the language, and using `arguments` was the only way to get all arguments of the function, no matter their total number.
100
-
101
-
And it still works, we can use it today.
99
+
In old times, rest parameters did not exist in the language, and using `arguments` was the only way to get all arguments of the function. And it still works, we can find it in the old code.
102
100
103
101
But the downside is that although `arguments` is both array-like and iterable, it's not an array. It does not support array methods, so we can't call `arguments.map(...)` for example.
104
102
@@ -119,11 +117,12 @@ function f() {
119
117
120
118
f(1); // 1
121
119
```
122
-
````
123
120
124
121
As we remember, arrow functions don't have their own `this`. Now we know they don't have the special `arguments` object either.
122
+
````
125
123
126
-
## Spread operator [#spread-operator]
124
+
125
+
## Spread syntax [#spread-syntax]
127
126
128
127
We've just seen how to get an array from the list of parameters.
129
128
@@ -149,7 +148,7 @@ alert( Math.max(arr) ); // NaN
149
148
150
149
And surely we can't manually list items in the code `Math.max(arr[0], arr[1], arr[2])`, because we may be unsure how many there are. As our script executes, there could be a lot, or there could be none. And that would get ugly.
151
150
152
-
*Spread operator* to the rescue! It looks similar to rest parameters, also using `...`, but does quite the opposite.
151
+
*Spread syntax* to the rescue! It looks similar to rest parameters, also using `...`, but does quite the opposite.
153
152
154
153
When `...arr` is used in the function call, it "expands" an iterable object `arr` into the list of arguments.
155
154
@@ -170,7 +169,7 @@ let arr2 = [8, 3, -8, 1];
170
169
alert( Math.max(...arr1, ...arr2) ); // 8
171
170
```
172
171
173
-
We can even combine the spread operator with normal values:
172
+
We can even combine the spread syntax with normal values:
alert(merged); // 0,3,5,1,2,8,9,15 (0, then arr, then 2, then arr2)
194
193
```
195
194
196
-
In the examples above we used an array to demonstrate the spread operator, but any iterable will do.
195
+
In the examples above we used an array to demonstrate the spread syntax, but any iterable will do.
197
196
198
-
For instance, here we use the spread operator to turn the string into array of characters:
197
+
For instance, here we use the spread syntax to turn the string into array of characters:
199
198
200
199
```js run
201
200
let str = "Hello";
202
201
203
202
alert( [...str] ); // H,e,l,l,o
204
203
```
205
204
206
-
The spread operator internally uses iterators to gather elements, the same way as `for..of` does.
205
+
The spread syntax internally uses iterators to gather elements, the same way as `for..of` does.
207
206
208
207
So, for a string, `for..of` returns characters and `...str` becomes `"H","e","l","l","o"`. The list of characters is passed to array initializer `[...str]`.
209
208
@@ -221,24 +220,69 @@ The result is the same as `[...str]`.
221
220
But there's a subtle difference between `Array.from(obj)` and `[...obj]`:
222
221
223
222
- `Array.from` operates on both array-likes and iterables.
224
-
- The spread operator operates only on iterables.
223
+
- The spread syntax works only with iterables.
225
224
226
225
So, for the task of turning something into an array, `Array.from` tends to be more universal.
227
226
228
227
228
+
## Get a new copy of an array/object
229
+
230
+
Remember when we talked about `Object.assign()` [in the past](https://javascript.info/object#cloning-and-merging-object-assign)?
231
+
232
+
It is possible to do the same thing with the spread syntax.
233
+
234
+
```js run
235
+
let arr = [1, 2, 3];
236
+
let arrCopy = [...arr]; // spread the array into a list of parameters
This way of copying an object is much shorter than `let objCopy = Object.assign({}, obj);` or for an array `let arrCopy = Object.assign([], arr);` so we prefer to use it whenever we can.
271
+
272
+
229
273
## Summary
230
274
231
-
When we see `"..."` in the code, it is either rest parameters or the spread operator.
275
+
When we see `"..."` in the code, it is either rest parameters or the spread syntax.
232
276
233
277
There's an easy way to distinguish between them:
234
278
235
279
- When `...` is at the end of function parameters, it's "rest parameters" and gathers the rest of the list of arguments into an array.
236
-
- When `...` occurs in a function call or alike, it's called a "spread operator" and expands an array into a list.
280
+
- When `...` occurs in a function call or alike, it's called a "spread syntax" and expands an array into a list.
237
281
238
282
Use patterns:
239
283
240
284
- Rest parameters are used to create functions that accept any number of arguments.
241
-
- The spread operator is used to pass an array to functions that normally require a list of many arguments.
285
+
- The spread syntax is used to pass an array to functions that normally require a list of many arguments.
242
286
243
287
Together they help to travel between a list and an array of parameters with ease.
0 commit comments