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
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -35,15 +35,15 @@ The terms above are good to remember because they are used in developer articles
35
35
Engines are complicated. But the basics are easy.
36
36
37
37
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.
39
39
3. And then the machine code runs, pretty fast.
40
40
41
41
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.
42
42
```
43
43
44
44
## What can in-browser JavaScript do?
45
45
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.
47
47
48
48
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.
49
49
@@ -59,25 +59,25 @@ For instance, in-browser JavaScript is able to:
59
59
60
60
## What CAN'T in-browser JavaScript do?
61
61
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.
63
63
64
64
Examples of such restrictions include:
65
65
66
66
- 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.
67
67
68
68
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.
69
69
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).
72
72
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.
74
74
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.
76
76
- 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.
77
77
78
78

79
79
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.
81
81
82
82
## What makes JavaScript unique?
83
83
@@ -92,28 +92,28 @@ JavaScript is the only browser technology that combines these three things.
92
92
93
93
That's what makes JavaScript unique. That's why it's the most widespread tool for creating browser interfaces.
94
94
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.
96
96
97
97
## Languages "over" JavaScript
98
98
99
99
The syntax of JavaScript does not suit everyone's needs. Different people want different features.
100
100
101
101
That's to be expected, because projects and requirements are different for everyone.
102
102
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.
104
104
105
105
Modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language and auto-converting it "under the hood".
106
106
107
107
Examples of such languages:
108
108
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.
110
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
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.
115
115
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.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/07-type-conversions/article.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ Numeric conversion rules:
70
70
|`undefined`|`NaN`|
71
71
|`null`|`0`|
72
72
|<code>true and 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`. |
74
74
75
75
Examples:
76
76
@@ -130,7 +130,7 @@ The conversion follows the rules:
130
130
|`undefined`|`NaN`|
131
131
|`null`|`0`|
132
132
|<code>true / 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`. |
134
134
135
135
**`Boolean Conversion`** -- Occurs in logical operations. Can be performed with `Boolean(value)`.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/08-operators/article.md
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -194,18 +194,18 @@ Here's an extract from the [precedence table](https://developer.mozilla.org/en-U
194
194
| Precedence | Name | Sign |
195
195
|------------|------|------|
196
196
|...|...|...|
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 |`-`|
204
204
|...|...|...|
205
205
|2| assignment |`=`|
206
206
|...|...|...|
207
207
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.
209
209
210
210
## Assignment
211
211
@@ -303,9 +303,9 @@ Such operators have the same precedence as a normal assignment, so they run afte
303
303
```js run
304
304
let n = 2;
305
305
306
-
n *= 3 + 5;
306
+
n *= 3 + 5; // right part evaluated first, same as n *= 8
307
307
308
-
alert( n ); // 16 (right part evaluated first, same as n *= 8)
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/12-nullish-coalescing-operator/article.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -106,7 +106,7 @@ In practice, the zero height is often a valid value, that shouldn't be replaced
106
106
107
107
## Precedence
108
108
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).
110
110
111
111
That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
@@ -206,7 +206,13 @@ function showMessage(from, *!*text = "no text given"*/!*) {
206
206
showMessage("Ann"); // Ann: no text given
207
207
```
208
208
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
+
```
210
216
211
217
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:
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/05-testing-mocha/article.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -79,7 +79,7 @@ So, the development is *iterative*. We write the spec, implement it, make sure t
79
79
80
80
Let's see this development flow in our practical case.
81
81
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).
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/09-object-toprimitive/article.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -136,8 +136,8 @@ As we can see from the code, `user` becomes a self-descriptive string or a money
136
136
137
137
If there's no `Symbol.toPrimitive` then JavaScript tries to find methods `toString` and `valueOf`:
138
138
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).
141
141
142
142
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.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/article.md
+34-3Lines changed: 34 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -334,15 +334,44 @@ alert( isFinite(num) );
334
334
335
335
Please note that an empty or a space-only string is treated as `0` in all numeric functions including `isFinite`.
336
336
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`"
338
367
There is a special built-in method `Object.is` that compares values like `===`, but is more reliable for two edge cases:
339
368
340
369
1. It works with `NaN`: `Object.is(NaN, NaN) === true`, that's a good thing.
341
370
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.
342
371
343
372
In all other cases, `Object.is(a, b)` is the same as `a === b`.
344
373
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)).
346
375
```
347
376
348
377
@@ -435,7 +464,9 @@ For different numeral systems:
435
464
For regular number tests:
436
465
437
466
- `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`
439
470
440
471
For converting values like `12pt` and `100px` to a number:
Copy file name to clipboardExpand all lines: 1-js/05-data-types/03-string/article.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -445,6 +445,8 @@ There are 3 methods in JavaScript to get a substring: `substring`, `substr` and
445
445
alert( str.substr(-4, 2) ); // 'gi', from the 4th position get 2 characters
446
446
```
447
447
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.
Copy file name to clipboardExpand all lines: 1-js/06-advanced-functions/01-recursion/01-sum-to/solution.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -37,4 +37,4 @@ P.S. Naturally, the formula is the fastest solution. It uses only 3 operations f
37
37
38
38
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.
39
39
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