You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/01-getting-started/2-manuals-specifications/article.md
+2-3Lines changed: 2 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -3,16 +3,15 @@
3
3
4
4
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.
5
5
6
-
7
6
## Specification
8
7
9
8
**The ECMA-262 specification** contains the most in-depth, detailed and formalized information about JavaScript. It defines the language.
10
9
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.
12
11
13
12
The latest draft is at <https://tc39.es/ecma262/>.
14
13
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>.
16
15
17
16
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.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/09-alert-prompt-confirm/article.md
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -30,15 +30,15 @@ The function `prompt` accepts two arguments:
30
30
result =prompt(title, [default]);
31
31
```
32
32
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.
34
34
35
35
`title`
36
36
: The text to show the visitor.
37
37
38
38
`default`
39
39
: An optional second parameter, the initial value for the input field.
40
40
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.
42
42
43
43
The call to `prompt` returns the text from the input field or `null` if the input was canceled.
44
44
@@ -74,7 +74,7 @@ The syntax:
74
74
result = confirm(question);
75
75
```
76
76
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.
78
78
79
79
The result is `true` if OK is pressed and `false` otherwise.
80
80
@@ -94,10 +94,10 @@ We covered 3 browser-specific functions to interact with visitors:
94
94
: shows a message.
95
95
96
96
`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`.
98
98
99
99
`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`.
101
101
102
102
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.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/03-string/article.md
+35-36Lines changed: 35 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ let double = "double-quoted";
17
17
let backticks =`backticks`;
18
18
```
19
19
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 `${…}`:
21
21
22
22
```js run
23
23
functionsum(a, b) {
@@ -39,48 +39,56 @@ let guestList = `Guests:
39
39
alert(guestList); // a list of guests, multiple lines
40
40
```
41
41
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
+
43
46
```js run
44
-
let guestList ="Guests: // Error: Unexpected token ILLEGAL
47
+
let guestList ="Guests: // Error: Unexpected token ILLEGAL
45
48
* John";
46
49
```
47
50
48
51
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.
49
52
50
53
Backticks also allow us to specify a "template function" before the first backtick. The syntax is: <code>func`string`</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.
51
54
52
-
53
55
## Special characters
54
56
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:
56
58
57
59
```js run
58
60
let guestList ="Guests:\n * John\n * Pete\n * Mary";
59
61
60
62
alert(guestList); // a multiline list of guests
61
63
```
62
64
63
-
For example, these two lines describe the same:
65
+
For example, these two lines are equal, just written differently:
64
66
65
67
```js run
66
-
alert( "Hello\nWorld" ); // two lines using a "newline symbol"
68
+
let str1 ="Hello\nWorld"; // two lines using a "newline symbol"
67
69
68
70
// 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
71
75
```
72
76
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:
74
80
75
81
| Character | Description |
76
82
|-----------|-------------|
77
-
|`\b`|Backspace|
78
-
|`\f`|Form feed|
79
83
|`\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. |
|`\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. |
84
92
85
93
Examples with unicode:
86
94
@@ -102,7 +110,7 @@ alert( 'I*!*\'*/!*m the Walrus!' ); // *!*I'm*/!* the Walrus!
102
110
103
111
As you can see, we have to prepend the inner quote by the backslash `\'`, because otherwise it would indicate the string end.
104
112
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:
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:
247
253
248
-
249
254
```js run
250
255
let str ='As sly as a fox, as strong as an ox';
251
256
@@ -367,7 +372,7 @@ The methods [str.startsWith](mdn:js/String/startsWith) and [str.endsWith](mdn:js
367
372
368
373
```js run
369
374
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"
371
376
```
372
377
373
378
## Getting a substring
@@ -401,15 +406,13 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
401
406
alert( str.slice(-4, -1) ); // gif
402
407
```
403
408
404
-
405
409
`str.substring(start [, end])`
406
410
: Returns the part of the string *between*`start` and `end`.
407
411
408
412
This is almost the same as `slice`, but it allows `start` to be greater than `end`.
409
413
410
414
For instance:
411
415
412
-
413
416
```js run
414
417
let str = "st*!*ring*/!*ify";
415
418
@@ -425,7 +428,6 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
425
428
426
429
Negative arguments are (unlike slice) not supported, they are treated as `0`.
427
430
428
-
429
431
`str.substr(start [, length])`
430
432
: Returns the part of the string from `start`, with the given `length`.
431
433
@@ -451,11 +453,10 @@ Let's recap these methods to avoid any confusion:
451
453
|`substring(start, end)`| between `start` and `end`| negative values mean `0`|
452
454
|`substr(start, length)`| from `start` get `length` characters | allows negative `start`|
453
455
454
-
455
456
```smart header="Which one to choose?"
456
457
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.
457
458
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.
459
460
```
460
461
461
462
## Comparing strings
@@ -527,10 +528,9 @@ The characters are compared by their numeric code. The greater code means that t
527
528
- All lowercase letters go after uppercase letters because their codes are greater.
528
529
- Some letters like `Ö` stand apart from the main alphabet. Here, it's code is greater than anything from `a` to `z`.
529
530
530
-
531
531
### Correct comparisons
532
532
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.
534
534
535
535
So, the browser needs to know the language to compare.
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.
554
554
555
555
## Internals, Unicode
556
556
@@ -580,7 +580,7 @@ We actually have a single symbol in each of the strings above, but the `length`
580
580
581
581
`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.
582
582
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:
584
584
585
585
```js run
586
586
alert( '𝒳'[0] ); // strange symbols...
@@ -608,7 +608,7 @@ In many languages there are symbols that are composed of the base character with
608
608
609
609
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.
610
610
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.
612
612
613
613
For instance, if we have `S` followed by the special "dot above" character (code `\u0307`), it is shown as Ṡ.
alert( 'S\u0307\u0323'=='S\u0323\u0307' ); // false, different characters (?!)
638
638
```
639
639
640
640
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 `Ṩ`
657
657
658
658
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.
659
659
660
-
661
660
## Summary
662
661
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`${…}`.
664
663
- Strings in JavaScript are encoded using UTF-16.
665
664
- We can use special characters like `\n` and insert letters by their unicode using `\u...`.
666
665
- To get a character, use: `[]`.
@@ -673,6 +672,6 @@ There are several other helpful methods in strings:
673
672
674
673
-`str.trim()` -- removes ("trims") spaces from the beginning and end of the string.
675
674
-`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).
677
676
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>.
Copy file name to clipboardExpand all lines: 1-js/11-async/03-promise-chaining/article.md
+3-5Lines changed: 3 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -146,7 +146,7 @@ Returning promises allows us to build chains of asynchronous actions.
146
146
147
147
## Example: loadScript
148
148
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:
150
150
151
151
```js run
152
152
loadScript("/article/promise-chaining/one.js")
@@ -207,9 +207,7 @@ Sometimes it's ok to write `.then` directly, because the nested function has acc
207
207
208
208
209
209
````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.
213
211
214
212
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`.
215
213
@@ -244,7 +242,7 @@ This feature allows to integrate custom objects with promise chains without havi
244
242
245
243
In frontend programming promises are often used for network requests. So let's see an extended example of that.
246
244
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:
Copy file name to clipboardExpand all lines: 1-js/13-modules/01-modules-intro/article.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -296,7 +296,7 @@ There are two notable differences of external module scripts:
296
296
<script type="module" src="my.js"></script>
297
297
```
298
298
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.
300
300
```html
301
301
<!-- another-site.com must supply Access-Control-Allow-Origin -->
0 commit comments