Skip to content

Commit cbbe5b1

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-468e3552
2 parents 47e2096 + 468e355 commit cbbe5b1

File tree

21 files changed

+187
-161
lines changed

21 files changed

+187
-161
lines changed

1-js/01-getting-started/1-intro/article.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Examples of such languages:
111111
- [Flow](http://flow.org/) also adds data typing, but in a different way. Developed by Facebook.
112112
- [Dart](https://www.dartlang.org/) is a standalone language that has its own engine that runs in non-browser environments (like mobile apps), but also can be transpiled to JavaScript. Developed by Google.
113113
- [Brython](https://brython.info/) is a Python transpiler to JavaScript that enables the writing of applications in pure Python without JavaScript.
114+
- [Kotlin](https://kotlinlang.org/docs/reference/js-overview.html) is a modern, concise and safe programming language that can target the browser or Node.
114115

115116
There are more. Of course, even if we use one of transpiled languages, we should also know JavaScript to really understand what we're doing.
116117

1-js/02-first-steps/08-operators/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ alert( x ); // 5
219219
220220
The fact of `=` being an operator, not a "magical" language construct has an interesting implication.
221221
222-
Most operators in JavaScript return a value. That's obvious for `+` and `-`, but also true for `=`.
222+
All operators in JavaScript return a value. That's obvious for `+` and `-`, but also true for `=`.
223223

224224
The call `x = value` writes the `value` into `x` *and then returns it*.
225225

1-js/02-first-steps/18-javascript-specials/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ We covered three ways to create a function in JavaScript:
273273
```
274274
275275
276-
- Functions may have local variables: those declared inside its body. Such variables are only visible inside the function.
276+
- Functions may have local variables: those declared inside its body or its parameter list. Such variables are only visible inside the function.
277277
- Parameters can have default values: `function sum(a = 1, b = 2) {...}`.
278278
- Functions always return something. If there's no `return` statement, then the result is `undefined`.
279279

1-js/03-code-quality/01-debugging-chrome/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ There are buttons for it at the top of the right panel. Let's engage them.
135135
Clicking this again and again will step through all script statements one by one.
136136

137137
<span class="devtools" style="background-position:-62px -192px"></span> -- "Step over": run the next command, but *don't go into a function*, hotkey `key:F10`.
138-
: Similar to the previous the "Step" command, but behaves differently if the next statement is a function call. That is: not a built-in, like `alert`, but a function of our own.
138+
: Similar to the previous "Step" command, but behaves differently if the next statement is a function call. That is: not a built-in, like `alert`, but a function of our own.
139139

140140
The "Step" command goes into it and pauses the execution at its first line, while "Step over" executes the nested function call invisibly, skipping the function internals.
141141

1-js/04-object-basics/07-optional-chaining/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,4 @@ As we can see, all of them are straightforward and simple to use. The `?.` check
219219
220220
A chain of `?.` allows to safely access nested properties.
221221
222-
Still, we should apply `?.` carefully, only where it's acceptable that the left part doesn't to exist. So that it won't hide programming errors from us, if they occur.
222+
Still, we should apply `?.` carefully, only where it's acceptable that the left part doesn't exist. So that it won't hide programming errors from us, if they occur.

1-js/05-data-types/02-number/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ JavaScript has a built-in [Math](https://developer.mozilla.org/en/docs/Web/JavaS
383383
A few examples:
384384

385385
`Math.random()`
386-
: Returns a random number from 0 to 1 (not including 1)
386+
: Returns a random number from 0 to 1 (not including 1).
387387

388388
```js run
389389
alert( Math.random() ); // 0.1234567894322
@@ -400,7 +400,7 @@ A few examples:
400400
```
401401

402402
`Math.pow(n, power)`
403-
: Returns `n` raised the given power
403+
: Returns `n` raised to the given power.
404404

405405
```js run
406406
alert( Math.pow(2, 10) ); // 2 in power 10 = 1024

1-js/05-data-types/06-iterable/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ When we use JavaScript for practical tasks in a browser or any other environment
174174

175175
For instance, strings are both iterable (`for..of` works on them) and array-like (they have numeric indexes and `length`).
176176

177-
But an iterable may not be array-like. And vice versa an array-like may not be iterable.
177+
But an iterable may be not array-like. And vice versa an array-like may be not iterable.
178178

179179
For example, the `range` in the example above is iterable, but not array-like, because it does not have indexed properties and `length`.
180180

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

Lines changed: 56 additions & 29 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,7 +234,7 @@ 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 an object-like "pattern" for corresponding properties. In the simplest case, that's a list of variable names in `{...}`.
213238

214239
For instance:
215240

@@ -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 = {

1-js/06-advanced-functions/04-var/article.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if (true) {
5252
}
5353

5454
*!*
55-
alert(test); // Error: test is not defined
55+
alert(test); // ReferenceError: test is not defined
5656
*/!*
5757
```
5858

@@ -82,7 +82,7 @@ function sayHi() {
8282
}
8383

8484
sayHi();
85-
alert(phrase); // Error: phrase is not defined
85+
alert(phrase); // ReferenceError: phrase is not defined
8686
```
8787

8888
As we can see, `var` pierces through `if`, `for` or other code blocks. That's because a long time ago in JavaScript, blocks had no Lexical Environments, and `var` is a remnant of that.
@@ -231,7 +231,7 @@ The Function Expression is wrapped with parenthesis `(function {...})`, because
231231

232232
```js run
233233
// Tries to declare and immediately call a function
234-
function() { // <-- Error: Function statements require a function name
234+
function() { // <-- SyntaxError: Function statements require a function name
235235

236236
var message = "Hello";
237237

1-js/06-advanced-functions/10-bind/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ The call to `mul.bind(null, 2)` creates a new function `double` that passes call
247247
248248
That's called [partial function application](https://en.wikipedia.org/wiki/Partial_application) -- we create a new function by fixing some parameters of the existing one.
249249
250-
Please note that here we actually don't use `this` here. But `bind` requires it, so we must put in something like `null`.
250+
Please note that we actually don't use `this` here. But `bind` requires it, so we must put in something like `null`.
251251
252252
The function `triple` in the code below triples the value:
253253

0 commit comments

Comments
 (0)