Skip to content

Commit 98ef0ca

Browse files
authored
Small readability changes
Small changes to improve readability. No changes to the meaning or to the example code have been made.
1 parent 0130a59 commit 98ef0ca

File tree

1 file changed

+19
-21
lines changed
  • 1-js/06-advanced-functions/02-rest-parameters-spread-operator

1 file changed

+19
-21
lines changed

1-js/06-advanced-functions/02-rest-parameters-spread-operator/article.md

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ For instance:
88
- `Object.assign(dest, src1, ..., srcN)` -- copies properties from `src1..N` into `dest`.
99
- ...and so on.
1010

11-
In this chapter we'll see how to do the same. And, more important, how to feel comfortable working with such functions and arrays.
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.
1212

1313
[cut]
1414

@@ -27,7 +27,7 @@ alert( sum(1, 2, 3, 4, 5) );
2727

2828
There will be no error because of "excessive" arguments. But of course in the result only the first two will be counted.
2929

30-
The rest parameters can be mentioned in a function definition with three dots `...`. They literally mean: "gather the remaining parameters into an array".
30+
The rest parameters can be mentioned in a function definition with three dots `...`. They literally mean "gather the remaining parameters into an array".
3131

3232
For instance, to gather all arguments into array `args`:
3333

@@ -45,9 +45,9 @@ alert( sumAll(1, 2) ); // 3
4545
alert( sumAll(1, 2, 3) ); // 6
4646
```
4747

48-
We can choose to get first parameters as variables, and gather only the rest.
48+
We can choose to get the first parameters as variables, and gather only the rest.
4949

50-
Here the first two arguments go into variables and the rest goes to `titles` array:
50+
Here the first two arguments go into variables and the rest go into `titles` array:
5151

5252
```js run
5353
function showName(firstName, lastName, ...titles) {
@@ -72,7 +72,7 @@ function f(arg1, ...rest, arg2) { // arg2 after ...rest ?!
7272
}
7373
```
7474
75-
The `...rest` must always be the last.
75+
The `...rest` must always be last.
7676
````
7777

7878
## The "arguments" variable
@@ -98,13 +98,13 @@ showName("Julius", "Caesar");
9898
showName("Ilya");
9999
```
100100

101-
In old times, rest parameters did not exist in the language, and `arguments` was the only way to get all arguments of the function no matter of their total number.
101+
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.
102102

103-
And it still works, we can use it.
103+
And it still works, we can use it today.
104104

105-
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 say call `arguments.map(...)`.
105+
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.
106106

107-
Also, it always has all arguments in it, we can't capture them partially, like we did with rest parameters.
107+
Also, it always contains all arguments. We can't capture them partially, like we did with rest parameters.
108108

109109
So when we need these features, then rest parameters are preferred.
110110

@@ -123,21 +123,19 @@ f(1); // 1
123123
```
124124
As we remember, arrow functions don't have their own `this`. Now we know they don't have the special `arguments` object either.
125125

126-
````
127-
128126
## Spread operator [#spread-operator]
129127

130128
We've just seen how to get an array from the list of parameters.
131129

132130
But sometimes we need to do exactly the reverse.
133131

134-
For instance, there's a built-in function [Math.max](mdn:js/Math/max) that returns the greatest number from the list:
132+
For instance, there's a built-in function [Math.max](mdn:js/Math/max) that returns the greatest number from a list:
135133

136134
```js run
137135
alert( Math.max(3, 5, 1) ); // 5
138136
```
139137

140-
Now let's say we have an array `[3, 5, 1]`. How to call `Math.max` with it?
138+
Now let's say we have an array `[3, 5, 1]`. How do we call `Math.max` with it?
141139

142140
Passing it "as it" won't work, because `Math.max` expects a list of numeric arguments, not a single array:
143141

@@ -149,9 +147,9 @@ alert( Math.max(arr) ); // NaN
149147
*/!*
150148
```
151149

152-
...And surely we can't manually list items in the code `Math.max(arg[0], arg[1], arg[2])`, because we may be unsure how much are there. As our script executes, there might be many, or there might be none. Also that would be ugly.
150+
And surely we can't manually list items in the code `Math.max(arg[0], arg[1], arg[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.
153151

154-
*Spread operator* to the rescue. It looks similar to rest parameters, also using `...`, but does quite the opposite.
152+
*Spread operator* to the rescue! It looks similar to rest parameters, also using `...`, but does quite the opposite.
155153

156154
When `...arr` is used in the function call, it "expands" an iterable object `arr` into the list of arguments.
157155

@@ -172,7 +170,7 @@ let arr2 = [8, 3, -8, 1];
172170
alert( Math.max(...arr1, ...arr2) ); // 8
173171
```
174172

175-
...And even combine the spread operator with normal values:
173+
We can even combine the spread operator with normal values:
176174

177175

178176
```js run
@@ -182,7 +180,7 @@ let arr2 = [8, 3, -8, 1];
182180
alert( Math.max(1, ...arr1, 2, ...arr2, 25) ); // 25
183181
```
184182

185-
Also spread operator can be used to merge arrays:
183+
Also, the spread operator can be used to merge arrays:
186184

187185
```js run
188186
let arr = [3, 5, 1];
@@ -197,7 +195,7 @@ alert(merged); // 0,3,5,1,2,8,9,15 (0, then arr, then 2, then arr2)
197195

198196
In the examples above we used an array to demonstrate the spread operator, but any iterable will do.
199197

200-
For instance, here we use spread operator to turn the string into array of characters:
198+
For instance, here we use the spread operator to turn the string into array of characters:
201199

202200
```js run
203201
let str = "Hello";
@@ -225,7 +223,7 @@ But there's a subtle difference between `Array.from(obj)` and `[...obj]`:
225223
- `Array.from` operates on both array-likes and iterables.
226224
- The spread operator operates only on iterables.
227225

228-
So, for the task of turning something into an array, `Array.from` appears more universal.
226+
So, for the task of turning something into an array, `Array.from` tends to be more universal.
229227

230228

231229
## Summary
@@ -234,8 +232,8 @@ When we see `"..."` in the code, it is either rest parameters or the spread oper
234232

235233
There's an easy way to distinguish between them:
236234

237-
- When `...` is at the end of function parameters, it's "rest parameters" and gathers the rest of the list into the array.
238-
- When `...` occurs in a function call or alike, it's called a "spread operator" and expands an array into the list.
235+
- 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.
239237

240238
Use patterns:
241239

0 commit comments

Comments
 (0)