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/1-intro/article.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -106,9 +106,9 @@ Modern tools make the transpilation very fast and transparent, actually allowing
106
106
107
107
Examples of such languages:
108
108
109
-
-[CoffeeScript](http://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.
110
-
-[TypeScript](http://www.typescriptlang.org/) is concentrated on adding "strict data typing" to simplify the development and support of complex systems. It is developed by Microsoft.
111
-
-[Flow](http://flow.org/) also adds data typing, but in a different way. Developed by Facebook.
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.
110
+
-[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.
111
+
-[Flow](https://flow.org/) also adds data typing, but in a different way. Developed by Facebook.
112
112
-[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.
113
113
-[Brython](https://brython.info/) is a Python transpiler to JavaScript that enables the writing of applications in pure Python without JavaScript.
114
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.
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.
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 resources.
5
5
6
6
## Specification
7
7
8
8
[The ECMA-262 specification](https://www.ecma-international.org/publications/standards/Ecma-262.htm) contains the most in-depth, detailed and formalized information about JavaScript. It defines the language.
9
9
10
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.
11
11
12
-
A new specification version is released every year. In-between these releases, the latest specification draft is at <https://tc39.es/ecma262/>.
12
+
A new specification version is released every year. Between these releases, the latest specification draft is at <https://tc39.es/ecma262/>.
13
13
14
14
To read about new bleeding-edge features, including those that are "almost standard" (so-called "stage 3"), see proposals at <https://github.com/tc39/proposals>.
15
15
@@ -19,19 +19,19 @@ Also, if you're developing for the browser, then there are other specifications
19
19
20
20
-**MDN (Mozilla) JavaScript Reference** is the main manual with examples and other information. It's great to get in-depth information about individual language functions, methods etc.
21
21
22
-
One can find it at <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference>.
22
+
You can find it at <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference>.
23
23
24
-
Although, it's often best to use an internet search instead. Just use "MDN [term]" in the query, e.g. <https://google.com/search?q=MDN+parseInt> to search for `parseInt` function.
24
+
Although, it's often best to use an internet search instead. Just use "MDN [term]" in the query, e.g. <https://google.com/search?q=MDN+parseInt> to search for the `parseInt` function.
25
25
26
26
## Compatibility tables
27
27
28
28
JavaScript is a developing language, new features get added regularly.
29
29
30
30
To see their support among browser-based and other engines, see:
31
31
32
-
-<http://caniuse.com> - per-feature tables of support, e.g. to see which engines support modern cryptography functions: <http://caniuse.com/#feat=cryptography>.
32
+
-<https://caniuse.com> - per-feature tables of support, e.g. to see which engines support modern cryptography functions: <https://caniuse.com/#feat=cryptography>.
33
33
-<https://kangax.github.io/compat-table> - a table with language features and engines that support those or don't support.
34
34
35
-
All these resources are useful in real-life development, as they contain valuable information about language details, their support etc.
35
+
All these resources are useful in real-life development, as they contain valuable information about language details, their support, etc.
36
36
37
37
Please remember them (or this page) for the cases when you need in-depth information about a particular feature.
For Windows, there's also "Visual Studio", not to be confused with "Visual Studio Code". "Visual Studio" is a paid and mighty Windows-only editor, well-suited for the .NET platform. It's also good at JavaScript. There's also a free version [Visual Studio Community](https://www.visualstudio.com/vs/community/).
19
19
@@ -31,7 +31,6 @@ In practice, lightweight editors may have a lot of plugins including directory-l
When a programmer is sure that a variable will never change, they can declare it with `const` to guarantee and clearly communicate that fact to everyone.
262
264
263
-
264
265
### Uppercase constants
265
266
266
267
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known prior to execution.
@@ -291,13 +292,14 @@ When should we use capitals for a constant and when should we name it normally?
291
292
Being a "constant" just means that a variable's value never changes. But there are constants that are known prior to execution (like a hexadecimal value for red) and there are constants that are *calculated* in run-time, during the execution, but do not change after their initial assignment.
292
293
293
294
For instance:
295
+
294
296
```js
295
297
const pageLoadTime = /* time taken by a webpage to load */;
296
298
```
297
299
298
300
The value of `pageLoadTime` is not known prior to the page load, so it's named normally. But it's still a constant because it doesn't change after assignment.
299
301
300
-
In other words, capital-named constants are only used as aliases for "hard-coded" values.
302
+
In other words, capital-named constants are only used as aliases for "hard-coded" values.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/05-types/article.md
+23-10Lines changed: 23 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,9 +68,20 @@ We'll see more about working with numbers in the chapter <info:number>.
68
68
69
69
## BigInt [#bigint-type]
70
70
71
-
In JavaScript, the "number" type cannot represent integer values larger than <code>(2<sup>53</sup>-1)</code> (that's `9007199254740991`), or less than <code>-(2<sup>53</sup>-1)</code>fornegatives. It's a technical limitation caused by their internal representation.
71
+
In JavaScript, the "number" type cannot safely represent integer values larger than <code>(2<sup>53</sup>-1)</code> (that's `9007199254740991`), or less than <code>-(2<sup>53</sup>-1)</code>for negatives.
72
72
73
-
For most purposes that's quite enough, but sometimes we need really big numbers, e.g. for cryptography or microsecond-precision timestamps.
73
+
To be really precise, the "number" type can store larger integers (up to <code>1.7976931348623157*10<sup>308</sup></code>), but outside of the safe integer range <code>±(2<sup>53</sup>-1)</code> there'll be a precision error, because not all digits fit into the fixed 64-bit storage. So an "approximate" value may be stored.
74
+
75
+
For example, these two numbers (right above the safe range) are the same:
So to say, all odd integers greater than <code>(2<sup>53</sup>-1)</code> can't be stored at all in the "number" type.
83
+
84
+
For most purposes <code>±(2<sup>53</sup>-1)</code> range is quite enough, but sometimes we need the entire range of really big integers, e.g. for cryptography or microsecond-precision timestamps.
74
85
75
86
`BigInt` type was recently added to the language to represent integers of arbitrary length.
76
87
@@ -263,14 +274,16 @@ Some people prefer `typeof(x)`, although the `typeof x` syntax is much more comm
263
274
264
275
There are 8 basic data types in JavaScript.
265
276
266
-
- `number` for numbers of any kind: integer or floating-point, integers are limited by <code>±(2<sup>53</sup>-1)</code>.
267
-
- `bigint` is for integer numbers of arbitrary length.
268
-
- `string` for strings. A string may have zero or more characters, there's no separate single-character type.
269
-
- `boolean` for `true`/`false`.
270
-
- `null` for unknown values -- a standalone type that has a single value `null`.
271
-
- `undefined` for unassigned values -- a standalone type that has a single value `undefined`.
272
-
- `object` for more complex data structures.
273
-
- `symbol` for unique identifiers.
277
+
- Seven primitive data types:
278
+
- `number` for numbers of any kind: integer or floating-point, integers are limited by <code>±(2<sup>53</sup>-1)</code>.
279
+
- `bigint` for integer numbers of arbitrary length.
280
+
- `string` for strings. A string may have zero or more characters, there's no separate single-character type.
281
+
- `boolean` for `true`/`false`.
282
+
- `null` for unknown values -- a standalone type that has a single value `null`.
283
+
- `undefined` for unassigned values -- a standalone type that has a single value `undefined`.
284
+
- `symbol` for unique identifiers.
285
+
- And one non-primitive data type:
286
+
- `object` for more complex data structures.
274
287
275
288
The `typeof` operator allows us to see which type is stored in a variable.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/13-while-for/article.md
+18-4Lines changed: 18 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,19 @@ For example, outputting goods from a list one after another or just running the
6
6
7
7
*Loops* are a way to repeat the same code multiple times.
8
8
9
+
```smart header="The for..of and for..in loops"
10
+
A small announcement for advanced readers.
11
+
12
+
This article covers only basic loops: `while`, `do..while` and `for(..;..;..)`.
13
+
14
+
If you came to this article searching for other types of loops, here are the pointers:
15
+
16
+
- See [for..in](info:object#forin) to loop over object properties.
17
+
- See [for..of](info:array#loops) and [iterables](info:iterable) for looping over arrays and iterable objects.
18
+
19
+
Otherwise, please read on.
20
+
```
21
+
9
22
## The "while" loop
10
23
11
24
The `while` loop has the following syntax:
@@ -162,10 +175,8 @@ for (i = 0; i < 3; i++) { // use an existing variable
162
175
163
176
alert(i); // 3, visible, because declared outside of the loop
164
177
```
165
-
166
178
````
167
179
168
-
169
180
### Skipping parts
170
181
171
182
Any part of `for` can be skipped.
@@ -268,7 +279,7 @@ for (let i = 0; i < 10; i++) {
268
279
269
280
From a technical point of view, this is identical to the example above. Surely, we can just wrap the code in an `if` block instead of using `continue`.
270
281
271
-
But as a side-effect, this created one more level of nesting (the `alert` call inside the curly braces). If the code inside of `if` is longer than a few lines, that may decrease the overall readability.
282
+
But as a sideeffect, this created one more level of nesting (the `alert` call inside the curly braces). If the code inside of `if` is longer than a few lines, that may decrease the overall readability.
272
283
````
273
284
274
285
````warn header="No `break/continue` to the right side of '?'"
@@ -286,7 +297,6 @@ if (i > 5) {
286
297
287
298
...and rewrite it using a question mark:
288
299
289
-
290
300
```js no-beautify
291
301
(i > 5) ? alert(i) : *!*continue*/!*; // continue isn't allowed here
292
302
```
@@ -321,6 +331,7 @@ We need a way to stop the process if the user cancels the input.
321
331
The ordinary `break` after `input` would only break the inner loop. That's not sufficient -- labels, come to the rescue!
322
332
323
333
A *label* is an identifier with a colon before a loop:
334
+
324
335
```js
325
336
labelName:for (...) {
326
337
...
@@ -342,6 +353,7 @@ The `break <labelName>` statement in the loop below breaks out to the label:
342
353
// do something with the value...
343
354
}
344
355
}
356
+
345
357
alert('Done!');
346
358
```
347
359
@@ -362,13 +374,15 @@ The `continue` directive can also be used with a label. In this case, code execu
362
374
Labels do not allow us to jump into an arbitrary place in the code.
363
375
364
376
For example, it is impossible to do this:
377
+
365
378
```js
366
379
break label; // jump to the label below (doesn't work)
367
380
368
381
label: for (...)
369
382
```
370
383
371
384
A `break` directive must be inside a code block. Technically, any labelled code block will do, e.g.:
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/14-switch/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,7 +139,7 @@ switch (a) {
139
139
140
140
Now both `3` and `5` show the same message.
141
141
142
-
The ability to "group" cases is a side-effect of how `switch/case` works without `break`. Here the execution of `case 3` starts from the line `(*)` and goes through `case 5`, because there's no `break`.
142
+
The ability to "group" cases is a sideeffect of how `switch/case` works without `break`. Here the execution of `case 3` starts from the line `(*)` and goes through `case 5`, because there's no `break`.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/15-function-basics/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -522,7 +522,7 @@ function name(parameters, delimited, by, comma) {
522
522
523
523
To make the code clean and easy to understand, it's recommended to use mainly local variables and parameters in the function, not outer variables.
524
524
525
-
It is always easier to understand a function which gets parameters, works with them and returns a result than a function which gets no parameters, but modifies outer variables as a side-effect.
525
+
It is always easier to understand a function which gets parameters, works with them and returns a result than a function which gets no parameters, but modifies outer variables as a sideeffect.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/16-function-expressions/article.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,7 +90,7 @@ Everything would work the same.
90
90
91
91
92
92
````smart header="Why is there a semicolon at the end?"
93
-
You might wonder, why does Function Expression have a semicolon `;` at the end, but Function Declaration does not:
93
+
You might wonder, why do Function Expressions have a semicolon `;` at the end, but Function Declarations do not:
94
94
95
95
```js
96
96
function sayHi() {
@@ -144,13 +144,13 @@ function showCancel() {
144
144
ask("Do you agree?", showOk, showCancel);
145
145
```
146
146
147
-
In practice, such functions are quite useful. The major difference between a real-life `ask` and the example above is that real-life functions use more complex ways to interact with the user than a simple `confirm`. In the browser, such function usually draws a nice-looking question window. But that's another story.
147
+
In practice, such functions are quite useful. The major difference between a real-life `ask` and the example above is that real-life functions use more complex ways to interact with the user than a simple `confirm`. In the browser, such functions usually draw a nice-looking question window. But that's another story.
148
148
149
149
**The arguments `showOk` and `showCancel` of `ask` are called *callback functions* or just *callbacks*.**
150
150
151
151
The idea is that we pass a function and expect it to be "called back" later if necessary. In our case, `showOk` becomes the callback for "yes" answer, and `showCancel` for "no" answer.
152
152
153
-
We can use Function Expressions to write the same function much shorter:
153
+
We can use Function Expressions to write an equivalent, shorter function:
154
154
155
155
```js run no-beautify
156
156
functionask(question, yes, no) {
@@ -186,7 +186,7 @@ Let's formulate the key differences between Function Declarations and Expression
186
186
187
187
First, the syntax: how to differentiate between them in the code.
188
188
189
-
-*Function Declaration:* a function, declared as a separate statement, in the main code flow.
189
+
-*Function Declaration:* a function, declared as a separate statement, in the main code flow:
0 commit comments