Skip to content

Commit c350514

Browse files
committed
minor
1 parent 42ee148 commit c350514

File tree

1 file changed

+57
-30
lines changed
  • 1-js/05-data-types/10-destructuring-assignment

1 file changed

+57
-30
lines changed

1-js/05-data-types/10-destructuring-assignment/article.md

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22

33
The two most used data structures in JavaScript are `Object` and `Array`.
44

5-
Objects allow us to create a single entity that stores data items by key, and arrays allow us to gather data items into an ordered collection.
5+
- Objects allow us to create a single entity that stores data items by key.
6+
- Arrays allow us to gather data items into an ordered list.
67

7-
But when we pass those to a function, it may need not an object/array as a whole, but rather individual pieces.
8+
Although, when we pass those to a function, it may need not an object/array as a whole. It may need individual pieces.
89

9-
*Destructuring assignment* is a special syntax that allows us to "unpack" arrays or objects into a bunch of variables, as sometimes that's more convenient. Destructuring also works great with complex functions that have a lot of parameters, default values, and so on.
10+
*Destructuring assignment* is a special syntax that allows us to "unpack" arrays or objects into a bunch of variables, as sometimes that's more convenient.
11+
12+
Destructuring also works great with complex functions that have a lot of parameters, default values, and so on. Soon we'll see that.
1013

1114
## Array destructuring
1215

13-
An example of how the array is destructured into variables:
16+
Here's an example of how an array is destructured into variables:
1417

1518
```js
1619
// we have an array with the name and surname
17-
let arr = ["Ilya", "Kantor"]
20+
let arr = ["John", "Smith"]
1821

1922
*!*
2023
// destructuring assignment
@@ -23,18 +26,22 @@ let arr = ["Ilya", "Kantor"]
2326
let [firstName, surname] = arr;
2427
*/!*
2528

26-
alert(firstName); // Ilya
27-
alert(surname); // Kantor
29+
alert(firstName); // John
30+
alert(surname); // Smith
2831
```
2932

3033
Now we can work with variables instead of array members.
3134

3235
It looks great when combined with `split` or other array-returning methods:
3336

34-
```js
35-
let [firstName, surname] = "Ilya Kantor".split(' ');
37+
```js run
38+
let [firstName, surname] = "John Smith".split(' ');
39+
alert(firstName); // John
40+
alert(surname); // Smith
3641
```
3742

43+
As you can see, the syntax is simple. There are several peculiar details though. Let's see more examples, to better understand it.
44+
3845
````smart header="\"Destructuring\" does not mean \"destructive\"."
3946
It's called "destructuring assignment," because it "destructurizes" by copying items into variables. But the array itself is not modified.
4047
@@ -69,26 +76,25 @@ In the code above, the second element of the array is skipped, the third one is
6976
let [a, b, c] = "abc"; // ["a", "b", "c"]
7077
let [one, two, three] = new Set([1, 2, 3]);
7178
```
72-
79+
That works, because internally a destructuring assignment works by iterating over the right value. It's kind of syntax sugar for calling `for..of` over the value to the right of `=` and assigning the values.
7380
````
7481

7582

7683
````smart header="Assign to anything at the left-side"
77-
7884
We can use any "assignables" at the left side.
7985
8086
For instance, an object property:
8187
```js run
8288
let user = {};
83-
[user.name, user.surname] = "Ilya Kantor".split(' ');
89+
[user.name, user.surname] = "John Smith".split(' ');
8490
85-
alert(user.name); // Ilya
91+
alert(user.name); // John
92+
alert(user.surname); // Smith
8693
```
8794
8895
````
8996

9097
````smart header="Looping with .entries()"
91-
9298
In the previous chapter we saw the [Object.entries(obj)](mdn:js/Object/entries) method.
9399
94100
We can use it with destructuring to loop over keys-and-values of an object:
@@ -107,62 +113,81 @@ for (let [key, value] of Object.entries(user)) {
107113
}
108114
```
109115
110-
...And the same for a map:
116+
The similar code for a `Map` is simpler, as it's iterable:
111117
112118
```js run
113119
let user = new Map();
114120
user.set("name", "John");
115121
user.set("age", "30");
116122
117123
*!*
124+
// Map iterates as [key, value] pairs, very convenient for destructuring
118125
for (let [key, value] of user) {
119126
*/!*
120127
alert(`${key}:${value}`); // name:John, then age:30
121128
}
122129
```
123130
````
124131

125-
```smart header="Swap variables trick"
126-
A well-known trick for swapping values of two variables:
132+
````smart header="Swap variables trick"
133+
There's a well-known trick for swapping values of two variables using a destructuring assignment:
127134
128135
```js run
129136
let guest = "Jane";
130137
let admin = "Pete";
131138
132-
// Swap values: make guest=Pete, admin=Jane
139+
// Let's swap the values: make guest=Pete, admin=Jane
140+
*!*
133141
[guest, admin] = [admin, guest];
142+
*/!*
134143
135144
alert(`${guest} ${admin}`); // Pete Jane (successfully swapped!)
136145
```
137146
138147
Here we create a temporary array of two variables and immediately destructure it in swapped order.
139148
140149
We can swap more than two variables this way.
141-
150+
````
142151

143152
### The rest '...'
144153

145-
If we want not just to get first values, but also to gather all that follows -- we can add one more parameter that gets "the rest" using three dots `"..."`:
154+
Usually, if the array is longer when the list at the left, the "extra" items are omitted.
155+
156+
For example, here only two items are taken, and the rest is just ignored:
146157

147158
```js run
148-
let [name1, name2, *!*...rest*/!*] = ["Julius", "Caesar", *!*"Consul", "of the Roman Republic"*/!*];
159+
let [name1, name2] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];
149160

150161
alert(name1); // Julius
151162
alert(name2); // Caesar
163+
// Furher items aren't assigned anywhere
164+
```
165+
166+
If we'd like also to gather all that follows -- we can add one more parameter that gets "the rest" using three dots `"..."`:
167+
168+
```js run
169+
let [name1, name2, *!*...rest*/!*] = ["Julius", "Caesar", *!*"Consul", "of the Roman Republic"*/!*];
152170
153171
*!*
154-
// Note that type of `rest` is Array.
172+
// rest is array of items, starting from the 3rd one
155173
alert(rest[0]); // Consul
156174
alert(rest[1]); // of the Roman Republic
157175
alert(rest.length); // 2
158176
*/!*
159177
```
160178

161-
The value of `rest` is the array of the remaining array elements. We can use any other variable name in place of `rest`, just make sure it has three dots before it and goes last in the destructuring assignment.
179+
The value of `rest` is the array of the remaining array elements.
180+
181+
We can use any other variable name in place of `rest`, just make sure it has three dots before it and goes last in the destructuring assignment.
182+
183+
```js run
184+
let [name1, name2, *!*...titles*/!*] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];
185+
// now titles = ["Consul", "of the Roman Republic"]
186+
```
162187

163188
### Default values
164189

165-
If there are fewer values in the array than variables in the assignment, there will be no error. Absent values are considered undefined:
190+
If the array is shorter than the list of variables at the left, there'll be no errors. Absent values are considered undefined:
166191

167192
```js run
168193
*!*
@@ -187,7 +212,7 @@ alert(surname); // Anonymous (default used)
187212

188213
Default values can be more complex expressions or even function calls. They are evaluated only if the value is not provided.
189214

190-
For instance, here we use the `prompt` function for two defaults. But it will run only for the missing one:
215+
For instance, here we use the `prompt` function for two defaults:
191216

192217
```js run
193218
// runs only prompt for surname
@@ -197,7 +222,7 @@ alert(name); // Julius (from array)
197222
alert(surname); // whatever prompt gets
198223
```
199224

200-
225+
Please note: the `prompt` will run only for the missing value (`surname`).
201226

202227
## Object destructuring
203228

@@ -209,9 +234,9 @@ The basic syntax is:
209234
let {var1, var2} = {var1:…, var2:…}
210235
```
211236

212-
We have an existing object at the right side, that we want to split into variables. The left side contains a "pattern" for corresponding properties. In the simple case, that's a list of variable names in `{...}`.
237+
We should have an existing object at the right side, that we want to split into variables. The left side contains a "pattern" for corresponding properties.
213238

214-
For instance:
239+
Usually, that's a list of variable names in `{...}`, for instance:
215240

216241
```js run
217242
let options = {
@@ -229,7 +254,9 @@ alert(width); // 100
229254
alert(height); // 200
230255
```
231256

232-
Properties `options.title`, `options.width` and `options.height` are assigned to the corresponding variables. The order does not matter. This works too:
257+
Properties `options.title`, `options.width` and `options.height` are assigned to the corresponding variables.
258+
259+
The order does not matter. This works too:
233260

234261
```js
235262
// changed the order in let {...}
@@ -238,7 +265,7 @@ let {height, width, title} = { title: "Menu", height: 200, width: 100 }
238265

239266
The pattern on the left side may be more complex and specify the mapping between properties and variables.
240267

241-
If we want to assign a property to a variable with another name, for instance, `options.width` to go into the variable named `w`, then we can set it using a colon:
268+
If we want to assign a property to a variable with another name, for instance, make `options.width` go into the variable named `w`, then we can set the variable name using a colon:
242269

243270
```js run
244271
let options = {

0 commit comments

Comments
 (0)