Skip to content

Commit d66a263

Browse files
committed
Merge branch 'master' of github.com:javascript-tutorial/en.javascript.info into sync-1edb0a38
2 parents dfe192a + 1edb0a3 commit d66a263

File tree

23 files changed

+122
-61
lines changed

23 files changed

+122
-61
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ The terms above are good to remember because they are used in developer articles
3535
Engines are complicated. But the basics are easy.
3636
3737
1. The engine (embedded if it's a browser) reads ("parses") the script.
38-
2. Then it converts ("compiles") the script to the machine language.
38+
2. Then it converts ("compiles") the script to machine code.
3939
3. And then the machine code runs, pretty fast.
4040
4141
The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and further optimizes the machine code based on that knowledge.
4242
```
4343

4444
## What can in-browser JavaScript do?
4545

46-
Modern JavaScript is a "safe" programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it.
46+
Modern JavaScript is a "safe" programming language. It does not provide low-level access to memory or the CPU, because it was initially created for browsers which do not require it.
4747

4848
JavaScript's capabilities greatly depend on the environment it's running in. For instance, [Node.js](https://wikipedia.org/wiki/Node.js) supports functions that allow JavaScript to read/write arbitrary files, perform network requests, etc.
4949

@@ -59,25 +59,25 @@ For instance, in-browser JavaScript is able to:
5959

6060
## What CAN'T in-browser JavaScript do?
6161

62-
JavaScript's abilities in the browser are limited for the sake of a user's safety. The aim is to prevent an evil webpage from accessing private information or harming the user's data.
62+
JavaScript's abilities in the browser are limited to protect the user's safety. The aim is to prevent an evil webpage from accessing private information or harming the user's data.
6363

6464
Examples of such restrictions include:
6565

6666
- JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS functions.
6767

6868
Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like "dropping" a file into a browser window or selecting it via an `<input>` tag.
6969

70-
There are ways to interact with camera/microphone and other devices, but they require a user's explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the [NSA](https://en.wikipedia.org/wiki/National_Security_Agency).
71-
- Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they come from different sites (from a different domain, protocol or port).
70+
There are ways to interact with the camera/microphone and other devices, but they require a user's explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the [NSA](https://en.wikipedia.org/wiki/National_Security_Agency).
71+
- Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other page if they come from different sites (from a different domain, protocol or port).
7272

73-
This is called the "Same Origin Policy". To work around that, *both pages* must agree for data exchange and contain a special JavaScript code that handles it. We'll cover that in the tutorial.
73+
This is called the "Same Origin Policy". To work around that, *both pages* must agree for data exchange and must contain special JavaScript code that handles it. We'll cover that in the tutorial.
7474

75-
This limitation is, again, for the user's safety. A page from `http://anysite.com` which a user has opened must not be able to access another browser tab with the URL `http://gmail.com` and steal information from there.
75+
This limitation is, again, for the user's safety. A page from `http://anysite.com` which a user has opened must not be able to access another browser tab with the URL `http://gmail.com`, for example, and steal information from there.
7676
- JavaScript can easily communicate over the net to the server where the current page came from. But its ability to receive data from other sites/domains is crippled. Though possible, it requires explicit agreement (expressed in HTTP headers) from the remote side. Once again, that's a safety limitation.
7777

7878
![](limitations.svg)
7979

80-
Such limits do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow plugin/extensions which may ask for extended permissions.
80+
Such limitations do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow plugins/extensions which may ask for extended permissions.
8181

8282
## What makes JavaScript unique?
8383

@@ -92,28 +92,28 @@ JavaScript is the only browser technology that combines these three things.
9292

9393
That's what makes JavaScript unique. That's why it's the most widespread tool for creating browser interfaces.
9494

95-
That said, JavaScript also allows to create servers, mobile applications, etc.
95+
That said, JavaScript can be used to create servers, mobile applications, etc.
9696

9797
## Languages "over" JavaScript
9898

9999
The syntax of JavaScript does not suit everyone's needs. Different people want different features.
100100

101101
That's to be expected, because projects and requirements are different for everyone.
102102

103-
So recently a plethora of new languages appeared, which are *transpiled* (converted) to JavaScript before they run in the browser.
103+
So, recently a plethora of new languages appeared, which are *transpiled* (converted) to JavaScript before they run in the browser.
104104

105105
Modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language and auto-converting it "under the hood".
106106

107107
Examples of such languages:
108108

109-
- [CoffeeScript](https://coffeescript.org/) is a "syntactic sugar" for JavaScript. It introduces shorter syntax, allowing us to write clearer and more precise code. Usually, Ruby devs like it.
109+
- [CoffeeScript](https://coffeescript.org/) is "syntactic sugar" for JavaScript. It introduces shorter syntax, allowing us to write clearer and more precise code. Usually, Ruby devs like it.
110110
- [TypeScript](https://www.typescriptlang.org/) is concentrated on adding "strict data typing" to simplify the development and support of complex systems. It is developed by Microsoft.
111111
- [Flow](https://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.
114114
- [Kotlin](https://kotlinlang.org/docs/reference/js-overview.html) is a modern, concise and safe programming language that can target the browser or Node.
115115

116-
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.
116+
There are more. Of course, even if we use one of these transpiled languages, we should also know JavaScript to really understand what we're doing.
117117

118118
## Summary
119119

1-js/02-first-steps/07-type-conversions/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Numeric conversion rules:
7070
|`undefined`|`NaN`|
7171
|`null`|`0`|
7272
|<code>true&nbsp;and&nbsp;false</code> | `1` and `0` |
73-
| `string` | Whitespaces from the start and end are removed. If the remaining string is empty, the result is `0`. Otherwise, the number is "read" from the string. An error gives `NaN`. |
73+
| `string` | Whitespaces (includes spaces, tabs `\t`, newlines `\n` etc.) from the start and end are removed. If the remaining string is empty, the result is `0`. Otherwise, the number is "read" from the string. An error gives `NaN`. |
7474

7575
Examples:
7676

@@ -130,7 +130,7 @@ The conversion follows the rules:
130130
|`undefined`|`NaN`|
131131
|`null`|`0`|
132132
|<code>true&nbsp;/&nbsp;false</code> | `1 / 0` |
133-
| `string` | The string is read "as is", whitespaces from both sides are ignored. An empty string becomes `0`. An error gives `NaN`. |
133+
| `string` | The string is read "as is", whitespaces (includes spaces, tabs `\t`, newlines `\n` etc.) from both sides are ignored. An empty string becomes `0`. An error gives `NaN`. |
134134
135135
**`Boolean Conversion`** -- Occurs in logical operations. Can be performed with `Boolean(value)`.
136136

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,18 @@ Here's an extract from the [precedence table](https://developer.mozilla.org/en-U
194194
| Precedence | Name | Sign |
195195
|------------|------|------|
196196
| ... | ... | ... |
197-
| 15 | unary plus | `+` |
198-
| 15 | unary negation | `-` |
199-
| 14 | exponentiation | `**` |
200-
| 13 | multiplication | `*` |
201-
| 13 | division | `/` |
202-
| 12 | addition | `+` |
203-
| 12 | subtraction | `-` |
197+
| 14 | unary plus | `+` |
198+
| 14 | unary negation | `-` |
199+
| 13 | exponentiation | `**` |
200+
| 12 | multiplication | `*` |
201+
| 12 | division | `/` |
202+
| 11 | addition | `+` |
203+
| 11 | subtraction | `-` |
204204
| ... | ... | ... |
205205
| 2 | assignment | `=` |
206206
| ... | ... | ... |
207207

208-
As we can see, the "unary plus" has a priority of `15` which is higher than the `12` of "addition" (binary plus). That's why, in the expression `"+apples + +oranges"`, unary pluses work before the addition.
208+
As we can see, the "unary plus" has a priority of `14` which is higher than the `11` of "addition" (binary plus). That's why, in the expression `"+apples + +oranges"`, unary pluses work before the addition.
209209
210210
## Assignment
211211
@@ -303,9 +303,9 @@ Such operators have the same precedence as a normal assignment, so they run afte
303303
```js run
304304
let n = 2;
305305
306-
n *= 3 + 5;
306+
n *= 3 + 5; // right part evaluated first, same as n *= 8
307307
308-
alert( n ); // 16 (right part evaluated first, same as n *= 8)
308+
alert( n ); // 16
309309
```
310310

311311
## Increment/decrement

1-js/02-first-steps/12-nullish-coalescing-operator/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ In practice, the zero height is often a valid value, that shouldn't be replaced
106106
107107
## Precedence
108108
109-
The precedence of the `??` operator is the same as `||`. They both equal `4` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
109+
The precedence of the `??` operator is the same as `||`. They both equal `3` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
110110
111111
That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
112112

1-js/02-first-steps/15-function-basics/article.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The `function` keyword goes first, then goes the *name of the function*, then a
2424

2525
```js
2626
function name(parameter1, parameter2, ... parameterN) {
27-
...body...
27+
// body
2828
}
2929
```
3030

@@ -206,7 +206,13 @@ function showMessage(from, *!*text = "no text given"*/!*) {
206206
showMessage("Ann"); // Ann: no text given
207207
```
208208
209-
Now if the `text` parameter is not passed, it will get the value `"no text given"`
209+
Now if the `text` parameter is not passed, it will get the value `"no text given"`.
210+
211+
The default value also jumps in if the parameter exists, but strictly equals `undefined`, like this:
212+
213+
```js
214+
showMessage("Ann", undefined); // Ann: no text given
215+
```
210216
211217
Here `"no text given"` is a string, but it can be a more complex expression, which is only evaluated and assigned if the parameter is missing. So, this is also possible:
212218

1-js/03-code-quality/05-testing-mocha/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ So, the development is *iterative*. We write the spec, implement it, make sure t
7979

8080
Let's see this development flow in our practical case.
8181

82-
The first step is already complete: we have an initial spec for `pow`. Now, before making the implementation, let's use few JavaScript libraries to run the tests, just to see that they are working (they will all fail).
82+
The first step is already complete: we have an initial spec for `pow`. Now, before making the implementation, let's use a few JavaScript libraries to run the tests, just to see that they are working (they will all fail).
8383

8484
## The spec in action
8585

1-js/04-object-basics/09-object-toprimitive/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ As we can see from the code, `user` becomes a self-descriptive string or a money
136136

137137
If there's no `Symbol.toPrimitive` then JavaScript tries to find methods `toString` and `valueOf`:
138138

139-
- For the `"string"` hint: call `toString` method, and if it doesn't exist, then `valueOf` (so `toString` has the priority for string conversions).
140-
- For other hints: `valueOf`, and if it doesn't exist, then `toString` (so `valueOf` has the priority for maths).
139+
- For the `"string"` hint: call `toString` method, and if it doesn't exist or if it returns an object instead of a primitive value, then call `valueOf` (so `toString` has the priority for string conversions).
140+
- For other hints: call `valueOf`, and if it doesn't exist or if it returns an object instead of a primitive value, then call `toString` (so `valueOf` has the priority for maths).
141141

142142
Methods `toString` and `valueOf` come from ancient times. They are not symbols (symbols did not exist that long ago), but rather "regular" string-named methods. They provide an alternative "old-style" way to implement the conversion.
143143

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,44 @@ alert( isFinite(num) );
334334

335335
Please note that an empty or a space-only string is treated as `0` in all numeric functions including `isFinite`.
336336

337-
```smart header="Compare with `Object.is`"
337+
````smart header="`Number.isNaN` and `Number.isFinite`"
338+
[Number.isNaN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN) and [Number.isFinite](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isFinite) methods are the more "strict" versions of `isNaN` and `isFinite` functions. They do not autoconvert their argument into a number, but check if it belongs to the `number` type instead.
339+
340+
- `Number.isNaN(value)` returns `true` if the argument belongs to the `number` type and it is `NaN`. In any other case it returns `false`.
341+
342+
```js run
343+
alert( Number.isNaN(NaN) ); // true
344+
alert( Number.isNaN("str" / 2) ); // true
345+
346+
// Note the difference:
347+
alert( Number.isNaN("str") ); // false, because "str" belongs to the string type, not the number type
348+
alert( isNaN("str") ); // true, because isNaN converts string "str" into a number and gets NaN as a result of this conversion
349+
```
350+
351+
- `Number.isFinite(value)` returns `true` if the argument belongs to the `number` type and it is not `NaN/Infinity/-Infinity`. In any other case it returns `false`.
352+
353+
```js run
354+
alert( Number.isFinite(123) ); // true
355+
alert( Number.isFinite(Infinity) ); //false
356+
alert( Number.isFinite(2 / 0) ); // false
357+
358+
// Note the difference:
359+
alert( Number.isFinite("123") ); // false, because "123" belongs to the string type, not the number type
360+
alert( isFinite("123") ); // true, because isFinite converts string "123" into a number 123
361+
```
362+
363+
In a way, `Number.isNaN` and `Number.isFinite` are simpler and more straightforward than `isNaN` and `isFinite` functions. In practice though, `isNaN` and `isFinite` are mostly used, as they're shorter to write.
364+
````
365+
366+
```smart header="Comparison with `Object.is`"
338367
There is a special built-in method `Object.is` that compares values like `===`, but is more reliable for two edge cases:
339368

340369
1. It works with `NaN`: `Object.is(NaN, NaN) === true`, that's a good thing.
341370
2. Values `0` and `-0` are different: `Object.is(0, -0) === false`, technically that's true, because internally the number has a sign bit that may be different even if all other bits are zeroes.
342371

343372
In all other cases, `Object.is(a, b)` is the same as `a === b`.
344373

345-
This way of comparison is often used in JavaScript specification. When an internal algorithm needs to compare two values for being exactly the same, it uses `Object.is` (internally called [SameValue](https://tc39.github.io/ecma262/#sec-samevalue)).
374+
We mention `Object.is` here, because it's often used in JavaScript specification. When an internal algorithm needs to compare two values for being exactly the same, it uses `Object.is` (internally called [SameValue](https://tc39.github.io/ecma262/#sec-samevalue)).
346375
```
347376

348377

@@ -435,7 +464,9 @@ For different numeral systems:
435464
For regular number tests:
436465

437466
- `isNaN(value)` converts its argument to a number and then tests it for being `NaN`
438-
- `isFinite(value)` converts its argument to a number and returns `true` if it's a regular number, not `NaN/Infinity/-Infinity`
467+
- `Number.isNaN(value)` checks whether its argument belongs to the `number` type, and if so, tests it for being `NaN`
468+
- `isFinite(value)` converts its argument to a number and then tests it for not being `NaN/Infinity/-Infinity`
469+
- `Number.isFinite(value)` checks whether its argument belongs to the `number` type, and if so, tests it for not being `NaN/Infinity/-Infinity`
439470

440471
For converting values like `12pt` and `100px` to a number:
441472

1-js/05-data-types/03-string/article.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
445445
alert( str.substr(-4, 2) ); // 'gi', from the 4th position get 2 characters
446446
```
447447

448+
This method resides in the [Annex B](https://tc39.es/ecma262/#sec-string.prototype.substr) of the language specification. It means that only browser-hosted Javascript engines should support it, and it's not recommended to use it. In practice, it's supported everywhere.
449+
448450
Let's recap these methods to avoid any confusion:
449451

450452
| method | selects... | negatives |

1-js/05-data-types/05-array-methods/article.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ The method [arr.lastIndexOf](mdn:js/Array/lastIndexOf) is the same as `indexOf`,
262262
```js run
263263
let fruits = ['Apple', 'Orange', 'Apple']
264264

265-
alert( arr.indexOf('Apple') ); // 0 (first Apple)
266-
alert( arr.lastIndexOf('Apple') ); // 2 (last Apple)
265+
alert( fruits.indexOf('Apple') ); // 0 (first Apple)
266+
alert( fruits.lastIndexOf('Apple') ); // 2 (last Apple)
267267
```
268268

269269
````smart header="The `includes` method handles `NaN` correctly"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ alert(arr.pop()); // World (method works)
218218

219219
The same happens for an iterable:
220220

221-
```js
221+
```js run
222222
// assuming that range is taken from the example above
223223
let arr = Array.from(range);
224224
alert(arr); // 1,2,3,4,5 (array toString conversion works)
@@ -233,7 +233,7 @@ The optional second argument `mapFn` can be a function that will be applied to e
233233

234234
For instance:
235235

236-
```js
236+
```js run
237237
// assuming that range is taken from the example above
238238

239239
// square each number

1-js/06-advanced-functions/01-recursion/01-sum-to/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ P.S. Naturally, the formula is the fastest solution. It uses only 3 operations f
3737

3838
The loop variant is the second in terms of speed. In both the recursive and the loop variant we sum the same numbers. But the recursion involves nested calls and execution stack management. That also takes resources, so it's slower.
3939

40-
P.P.S. Some engines support the "tail call" optimization: if a recursive call is the very last one in the function (like in `sumTo` above), then the outer function will not need to resume the execution, so the engine doesn't need to remember its execution context. That removes the burden on memory, so counting `sumTo(100000)` becomes possible. But if the JavaScript engine does not support tail call optimization (most of them don't), there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.
40+
P.P.S. Some engines support the "tail call" optimization: if a recursive call is the very last one in the function, with no other calculations performed, then the outer function will not need to resume the execution, so the engine doesn't need to remember its execution context. That removes the burden on memory. But if the JavaScript engine does not support tail call optimization (most of them don't), there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.

0 commit comments

Comments
 (0)