Skip to content

Commit 9a12fcf

Browse files
authored
Merge branch 'master' into patch-7
2 parents 4be6d6c + da23387 commit 9a12fcf

File tree

14 files changed

+129
-84
lines changed

14 files changed

+129
-84
lines changed

1-js/01-getting-started/2-manuals-specifications/article.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
This book is a *tutorial*. It aims to help you gradually learn the language. But once you're familiar with the basics, you'll need other sources.
55

6-
76
## Specification
87

98
**The ECMA-262 specification** contains the most in-depth, detailed and formalized information about JavaScript. It defines the language.
109

11-
But being that formalized, it's difficult to understand at first. So if you need the most trustworthy source of information about the language details, it's the right place. But it's not for everyday use.
10+
But being that formalized, it's difficult to understand at first. So if you need the most trustworthy source of information about the language details, the specification is the right place. But it's not for everyday use.
1211

1312
The latest draft is at <https://tc39.es/ecma262/>.
1413

15-
To read about bleeding-edge features, that are not yet widely supported, see proposals at <https://github.com/tc39/proposals>.
14+
To read about new bleeding-edge features, that are "almost standard", see proposals at <https://github.com/tc39/proposals>.
1615

1716
Also, if you're in developing for the browser, then there are other specs covered in the [second part](info:browser-environment) of the tutorial.
1817

1-js/02-first-steps/09-alert-prompt-confirm/article.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ The function `prompt` accepts two arguments:
3030
result = prompt(title, [default]);
3131
```
3232

33-
It shows a modal window with a text message, an input field for the visitor, and the buttons OK/CANCEL.
33+
It shows a modal window with a text message, an input field for the visitor, and the buttons OK/Cancel.
3434

3535
`title`
3636
: The text to show the visitor.
3737

3838
`default`
3939
: An optional second parameter, the initial value for the input field.
4040

41-
The visitor may type something in the prompt input field and press OK. Or they can cancel the input by pressing CANCEL or hitting the `key:Esc` key.
41+
The visitor may type something in the prompt input field and press OK. Or they can cancel the input by pressing Cancel or hitting the `key:Esc` key.
4242

4343
The call to `prompt` returns the text from the input field or `null` if the input was canceled.
4444

@@ -74,7 +74,7 @@ The syntax:
7474
result = confirm(question);
7575
```
7676
77-
The function `confirm` shows a modal window with a `question` and two buttons: OK and CANCEL.
77+
The function `confirm` shows a modal window with a `question` and two buttons: OK and Cancel.
7878
7979
The result is `true` if OK is pressed and `false` otherwise.
8080
@@ -94,10 +94,10 @@ We covered 3 browser-specific functions to interact with visitors:
9494
: shows a message.
9595
9696
`prompt`
97-
: shows a message asking the user to input text. It returns the text or, if CANCEL or `key:Esc` is clicked, `null`.
97+
: shows a message asking the user to input text. It returns the text or, if Cancel button or `key:Esc` is clicked, `null`.
9898
9999
`confirm`
100-
: shows a message and waits for the user to press "OK" or "CANCEL". It returns `true` for OK and `false` for CANCEL/`key:Esc`.
100+
: shows a message and waits for the user to press "OK" or "Cancel". It returns `true` for OK and `false` for Cancel/`key:Esc`.
101101
102102
All these methods are modal: they pause script execution and don't allow the visitor to interact with the rest of the page until the window has been dismissed.
103103

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

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let double = "double-quoted";
1717
let backticks = `backticks`;
1818
```
1919

20-
Single and double quotes are essentially the same. Backticks, however, allow us to embed any expression into the string, including function calls:
20+
Single and double quotes are essentially the same. Backticks, however, allow us to embed any expression into the string, by wrapping it in `${…}`:
2121

2222
```js run
2323
function sum(a, b) {
@@ -39,48 +39,56 @@ let guestList = `Guests:
3939
alert(guestList); // a list of guests, multiple lines
4040
```
4141

42-
If we try to use single or double quotes in the same way, there will be an error:
42+
Looks natural, right? But single or double quotes do not work this way.
43+
44+
If we use them and try to use multiple lines, there'll be an error:
45+
4346
```js run
44-
let guestList = "Guests: // Error: Unexpected token ILLEGAL
47+
let guestList = "Guests: // Error: Unexpected token ILLEGAL
4548
* John";
4649
```
4750

4851
Single and double quotes come from ancient times of language creation when the need for multiline strings was not taken into account. Backticks appeared much later and thus are more versatile.
4952

5053
Backticks also allow us to specify a "template function" before the first backtick. The syntax is: <code>func&#96;string&#96;</code>. The function `func` is called automatically, receives the string and embedded expressions and can process them. You can read more about it in the [docs](mdn:/JavaScript/Reference/Template_literals#Tagged_template_literals). This is called "tagged templates". This feature makes it easier to wrap strings into custom templating or other functionality, but it is rarely used.
5154

52-
5355
## Special characters
5456

55-
It is still possible to create multiline strings with single quotes by using a so-called "newline character", written as `\n`, which denotes a line break:
57+
It is still possible to create multiline strings with single and double quotes by using a so-called "newline character", written as `\n`, which denotes a line break:
5658

5759
```js run
5860
let guestList = "Guests:\n * John\n * Pete\n * Mary";
5961

6062
alert(guestList); // a multiline list of guests
6163
```
6264

63-
For example, these two lines describe the same:
65+
For example, these two lines are equal, just written differently:
6466

6567
```js run
66-
alert( "Hello\nWorld" ); // two lines using a "newline symbol"
68+
let str1 = "Hello\nWorld"; // two lines using a "newline symbol"
6769

6870
// two lines using a normal newline and backticks
69-
alert( `Hello
70-
World` );
71+
let str2 = `Hello
72+
World`;
73+
74+
alert(str1 == str2); // true
7175
```
7276

73-
There are other, less common "special" characters as well. Here's the list:
77+
There are other, less common "special" characters.
78+
79+
Here's the full list:
7480

7581
| Character | Description |
7682
|-----------|-------------|
77-
|`\b`|Backspace|
78-
|`\f`|Form feed|
7983
|`\n`|New line|
80-
|`\r`|Carriage return|
84+
|`\r`|Carriage return: not used alone. Windows text files use a combination of two characters `\n\r` to represent a line break. |
85+
|`\'`, `\"`|Quotes|
86+
|`\\`|Backslash|
8187
|`\t`|Tab|
82-
|`\uNNNN`|A unicode symbol with the hex code `NNNN`, for instance `\u00A9` -- is a unicode for the copyright symbol `©`. It must be exactly 4 hex digits. |
83-
|`\u{NNNNNNNN}`|Some rare characters are encoded with two unicode symbols, taking 4 bytes. This long unicode requires braces around it.|
88+
|`\b`, `\f`,`\v` | Backspace, Form Feed, Vertical Tab -- kept for compatibility, not used nowadays. |
89+
|`\xXX`|Unicode character with the given hexadimal unicode `XX`, e.g. `'\x7A'` is the same as `'z'`.|
90+
|`\uXXXX`|A unicode symbol with the hex code `XXXX` in UTF-16 encoding, for instance `\u00A9` -- is a unicode for the copyright symbol `©`. It must be exactly 4 hex digits. |
91+
|`\u{X…XXXXXX}` (1 to 6 hex characters)|A unicode symbol with the given UTF-32 encoding. Some rare characters are encoded with two unicode symbols, taking 4 bytes. This way we can insert long codes. |
8492

8593
Examples with unicode:
8694

@@ -102,7 +110,7 @@ alert( 'I*!*\'*/!*m the Walrus!' ); // *!*I'm*/!* the Walrus!
102110

103111
As you can see, we have to prepend the inner quote by the backslash `\'`, because otherwise it would indicate the string end.
104112

105-
Of course, that refers only to the quotes that are the same as the enclosing ones. So, as a more elegant solution, we could switch to double quotes or backticks instead:
113+
Of course, only to the quotes that are the same as the enclosing ones need to be escaped. So, as a more elegant solution, we could switch to double quotes or backticks instead:
106114

107115
```js run
108116
alert( `I'm the Walrus!` ); // I'm the Walrus!
@@ -120,7 +128,6 @@ alert( `The backslash: \\` ); // The backslash: \
120128

121129
## String length
122130

123-
124131
The `length` property has the string length:
125132

126133
```js run
@@ -189,7 +196,7 @@ For instance:
189196
```js run
190197
let str = 'Hi';
191198

192-
str = 'h' + str[1]; // replace the string
199+
str = 'h' + str[1]; // replace the string
193200

194201
alert( str ); // hi
195202
```
@@ -242,10 +249,8 @@ let str = 'Widget with id';
242249
alert( str.indexOf('id', 2) ) // 12
243250
```
244251

245-
246252
If we're interested in all occurrences, we can run `indexOf` in a loop. Every new call is made with the position after the previous match:
247253

248-
249254
```js run
250255
let str = 'As sly as a fox, as strong as an ox';
251256

@@ -367,7 +372,7 @@ The methods [str.startsWith](mdn:js/String/startsWith) and [str.endsWith](mdn:js
367372

368373
```js run
369374
alert( "Widget".startsWith("Wid") ); // true, "Widget" starts with "Wid"
370-
alert( "Widget".endsWith("get") ); // true, "Widget" ends with "get"
375+
alert( "Widget".endsWith("get") ); // true, "Widget" ends with "get"
371376
```
372377

373378
## Getting a substring
@@ -401,15 +406,13 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
401406
alert( str.slice(-4, -1) ); // gif
402407
```
403408

404-
405409
`str.substring(start [, end])`
406410
: Returns the part of the string *between* `start` and `end`.
407411

408412
This is almost the same as `slice`, but it allows `start` to be greater than `end`.
409413

410414
For instance:
411415

412-
413416
```js run
414417
let str = "st*!*ring*/!*ify";
415418

@@ -425,7 +428,6 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
425428

426429
Negative arguments are (unlike slice) not supported, they are treated as `0`.
427430

428-
429431
`str.substr(start [, length])`
430432
: Returns the part of the string from `start`, with the given `length`.
431433

@@ -451,11 +453,10 @@ Let's recap these methods to avoid any confusion:
451453
| `substring(start, end)` | between `start` and `end` | negative values mean `0` |
452454
| `substr(start, length)` | from `start` get `length` characters | allows negative `start` |
453455

454-
455456
```smart header="Which one to choose?"
456457
All of them can do the job. Formally, `substr` has a minor drawback: it is described not in the core JavaScript specification, but in Annex B, which covers browser-only features that exist mainly for historical reasons. So, non-browser environments may fail to support it. But in practice it works everywhere.
457458
458-
The author finds themself using `slice` almost all the time.
459+
Of the other two variants, `slice` is a little bit more flexible, it allows negative arguments and shorter to write. So, it's enough to remember solely `slice` of these three methods.
459460
```
460461

461462
## Comparing strings
@@ -527,10 +528,9 @@ The characters are compared by their numeric code. The greater code means that t
527528
- All lowercase letters go after uppercase letters because their codes are greater.
528529
- Some letters like `Ö` stand apart from the main alphabet. Here, it's code is greater than anything from `a` to `z`.
529530

530-
531531
### Correct comparisons
532532

533-
The "right" algorithm to do string comparisons is more complex than it may seem, because alphabets are different for different languages. The same-looking letter may be located differently in different alphabets.
533+
The "right" algorithm to do string comparisons is more complex than it may seem, because alphabets are different for different languages.
534534

535535
So, the browser needs to know the language to compare.
536536

@@ -550,7 +550,7 @@ For instance:
550550
alert( 'Österreich'.localeCompare('Zealand') ); // -1
551551
```
552552

553-
This method actually has two additional arguments specified in [the documentation](mdn:js/String/localeCompare), which allows it to specify the language (by default taken from the environment) and setup additional rules like case sensitivity or should `"a"` and `"á"` be treated as the same etc.
553+
This method actually has two additional arguments specified in [the documentation](mdn:js/String/localeCompare), which allows it to specify the language (by default taken from the environment, letter order depends on the language) and setup additional rules like case sensitivity or should `"a"` and `"á"` be treated as the same etc.
554554

555555
## Internals, Unicode
556556

@@ -580,7 +580,7 @@ We actually have a single symbol in each of the strings above, but the `length`
580580

581581
`String.fromCodePoint` and `str.codePointAt` are few rare methods that deal with surrogate pairs right. They recently appeared in the language. Before them, there were only [String.fromCharCode](mdn:js/String/fromCharCode) and [str.charCodeAt](mdn:js/String/charCodeAt). These methods are actually the same as `fromCodePoint/codePointAt`, but don't work with surrogate pairs.
582582

583-
But, for instance, getting a symbol can be tricky, because surrogate pairs are treated as two characters:
583+
Getting a symbol can be tricky, because surrogate pairs are treated as two characters:
584584

585585
```js run
586586
alert( '𝒳'[0] ); // strange symbols...
@@ -608,7 +608,7 @@ In many languages there are symbols that are composed of the base character with
608608

609609
For instance, the letter `a` can be the base character for: `àáâäãåā`. Most common "composite" character have their own code in the UTF-16 table. But not all of them, because there are too many possible combinations.
610610

611-
To support arbitrary compositions, UTF-16 allows us to use several unicode characters. The base character and one or many "mark" characters that "decorate" it.
611+
To support arbitrary compositions, UTF-16 allows us to use several unicode characters: the base character followed by one or many "mark" characters that "decorate" it.
612612

613613
For instance, if we have `S` followed by the special "dot above" character (code `\u0307`), it is shown as Ṡ.
614614

@@ -634,7 +634,7 @@ For instance:
634634
alert( 'S\u0307\u0323' ); // Ṩ, S + dot above + dot below
635635
alert( 'S\u0323\u0307' ); // Ṩ, S + dot below + dot above
636636

637-
alert( 'S\u0307\u0323' == 'S\u0323\u0307' ); // false
637+
alert( 'S\u0307\u0323' == 'S\u0323\u0307' ); // false, different characters (?!)
638638
```
639639

640640
To solve this, there exists a "unicode normalization" algorithm that brings each string to the single "normal" form.
@@ -657,10 +657,9 @@ In reality, this is not always the case. The reason being that the symbol `Ṩ`
657657

658658
If you want to learn more about normalization rules and variants -- they are described in the appendix of the Unicode standard: [Unicode Normalization Forms](http://www.unicode.org/reports/tr15/), but for most practical purposes the information from this section is enough.
659659

660-
661660
## Summary
662661

663-
- There are 3 types of quotes. Backticks allow a string to span multiple lines and embed expressions.
662+
- There are 3 types of quotes. Backticks allow a string to span multiple lines and embed expressions `${…}`.
664663
- Strings in JavaScript are encoded using UTF-16.
665664
- We can use special characters like `\n` and insert letters by their unicode using `\u...`.
666665
- To get a character, use: `[]`.
@@ -673,6 +672,6 @@ There are several other helpful methods in strings:
673672

674673
- `str.trim()` -- removes ("trims") spaces from the beginning and end of the string.
675674
- `str.repeat(n)` -- repeats the string `n` times.
676-
- ...and more. See the [manual](mdn:js/String) for details.
675+
- ...and more to be found in the [manual](mdn:js/String).
677676

678-
Strings also have methods for doing search/replace with regular expressions. But that topic deserves a separate chapter, so we'll return to that later.
677+
Strings also have methods for doing search/replace with regular expressions. But that's big topic, so it's explained in a separate tutorial section <info:regular-expressions>.

1-js/11-async/03-promise-chaining/article.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Returning promises allows us to build chains of asynchronous actions.
146146

147147
## Example: loadScript
148148

149-
Let's use this feature with the promisified `loadScript`, defined in the [previous chapter](/promise-basics#loadscript), to load scripts one by one, in sequence:
149+
Let's use this feature with the promisified `loadScript`, defined in the [previous chapter](info:promise-basics#loadscript), to load scripts one by one, in sequence:
150150

151151
```js run
152152
loadScript("/article/promise-chaining/one.js")
@@ -207,9 +207,7 @@ Sometimes it's ok to write `.then` directly, because the nested function has acc
207207

208208

209209
````smart header="Thenables"
210-
To be precise, `.then` may return an arbitrary "thenable" object, and it will be treated the same way as a promise.
211-
212-
A "thenable" object is any object with a method `.then`.
210+
To be precise, `.then` may return a so-called "thenable" object - an arbitrary object that has method `.then`, and it will be treated the same way as a promise.
213211
214212
The idea is that 3rd-party libraries may implement "promise-compatible" objects of their own. They can have extended set of methods, but also be compatible with native promises, because they implement `.then`.
215213
@@ -244,7 +242,7 @@ This feature allows to integrate custom objects with promise chains without havi
244242

245243
In frontend programming promises are often used for network requests. So let's see an extended example of that.
246244

247-
We'll use the [fetch](mdn:api/WindowOrWorkerGlobalScope/fetch) method to load the information about the user from the remote server. The method is quite complex, it has many optional parameters, but the basic usage is quite simple:
245+
We'll use the [fetch](info:fetch) method to load the information about the user from the remote server. It has a lot of optional parameters covered in separate chapters, but the basic syntax is quite simple:
248246

249247
```js
250248
let promise = fetch(url);

1-js/12-generators-iterators/2-async-iterators-generators/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ No problem, just prepend it with `async`, like this:
178178
})();
179179
```
180180

181-
Now we have an the async generator, iterable with `for await...of`.
181+
Now we have the async generator, iterable with `for await...of`.
182182

183183
It's indeed very simple. We add the `async` keyword, and the generator now can use `await` inside of it, rely on promises and other async functions.
184184

1-js/13-modules/01-modules-intro/article.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ There are two notable differences of external module scripts:
296296
<script type="module" src="my.js"></script>
297297
```
298298
299-
2. External scripts that are fetched from another domain require [CORS](mdn:Web/HTTP/CORS) headers. In other words, if a module script is fetched from another domain, the remote server must supply a header `Access-Control-Allow-Origin: *` (may use fetching domain instead of `*`) to indicate that the fetch is allowed.
299+
2. External scripts that are fetched from another origin (e.g. another site) require [CORS](mdn:Web/HTTP/CORS) headers, as described in the chapter <info:fetch-crossorigin>. In other words, if a module script is fetched from another origin, the remote server must supply a header `Access-Control-Allow-Origin: *` (may use site domain instead of `*`) to indicate that the fetch is allowed.
300300
```html
301301
<!-- another-site.com must supply Access-Control-Allow-Origin -->
302302
<!-- otherwise, the script won't execute -->

1-js/13-modules/02-import-export/article.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,20 @@ For instance, these are all perfectly valid default exports:
209209
export default class { // no class name
210210
constructor() { ... }
211211
}
212+
```
212213

214+
```js
213215
export default function(user) { // no function name
214216
alert(`Hello, ${user}!`);
215217
}
218+
```
216219

220+
```js
217221
// export a single value, without making a variable
218222
export default ['Jan', 'Feb', 'Mar','Apr', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
219223
```
220224

221-
That's fine, because `export default` is only one per file. Contrary to that, omitting a name for named imports would be an error:
225+
Not giving a name is fine, because `export default` is only one per file. Contrary to that, omitting a name for named imports would be an error:
222226

223227
```js
224228
export class { // Error! (non-default export needs a name)

0 commit comments

Comments
 (0)