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/02-first-steps/04-variables/article.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -88,16 +88,16 @@ In older scripts, you may also find another keyword: `var` instead of `let`:
88
88
*!*var*/!* message ='Hello';
89
89
```
90
90
91
-
The `var` keyword is *almost* the same as `let`. It also declares a variable, but in a slightly different, "old-school" way.
91
+
The `var` keyword is *almost* the same as `let`. It also declares a variable but in a slightly different, "old-school" way.
92
92
93
-
There are subtle differences between `let` and `var`, but they do not matter for us yet. We'll cover them in detail in the chapter <info:var>.
93
+
There are subtle differences between `let` and `var`, but they do not matter to us yet. We'll cover them in detail in the chapter <info:var>.
94
94
````
95
95
96
96
## A real-life analogy
97
97
98
98
We can easily grasp the concept of a "variable" if we imagine it as a "box" for data, with a uniquely-named sticker on it.
99
99
100
-
For instance, the variable `message` can be imagined as a box labeled `"message"` with the value `"Hello!"` in it:
100
+
For instance, the variable `message` can be imagined as a box labelled `"message"` with the value `"Hello!"` in it:
101
101
102
102

103
103
@@ -198,14 +198,14 @@ Variables named `apple` and `APPLE` are two different variables.
198
198
```
199
199
200
200
````smart header="Non-Latin letters are allowed, but not recommended"
201
-
It is possible to use any language, including cyrillic letters, Chinese logograms and so on, like this:
201
+
It is possible to use any language, including Cyrillic letters, Chinese logograms and so on, like this:
202
202
203
203
```js
204
204
let имя = '...';
205
205
let 我 = '...';
206
206
```
207
207
208
-
Technically, there is no error here. Such names are allowed, but there is an international convention to use English in variable names. Even if we're writing a small script, it may have a long life ahead. People from other countries may need to read it some time.
208
+
Technically, there is no error here. Such names are allowed, but there is an international convention to use English in variable names. Even if we're writing a small script, it may have a long life ahead. People from other countries may need to read it sometime.
myBirthday = '01.01.2001'; // error, can't reassign the constant!
261
261
```
262
262
263
-
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.
263
+
When a programmer is sure that a variable will never change, they can declare it with `const` to guarantee and communicate that fact to everyone.
264
264
265
265
### Uppercase constants
266
266
267
-
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known prior to execution.
267
+
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known before execution.
268
268
269
269
Such constants are named using capital letters and underscores.
270
270
@@ -289,15 +289,15 @@ Benefits:
289
289
290
290
When should we use capitals for a constant and when should we name it normally? Let's make that clear.
291
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
+
Being a "constant" just means that a variable's value never changes. But some constants are known before execution (like a hexadecimal value for red) and some constants are *calculated* in run-time, during the execution, but do not change after their initial assignment.
293
293
294
294
For instance:
295
295
296
296
```js
297
297
const pageLoadTime = /* time taken by a webpage to load */;
298
298
```
299
299
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.
300
+
The value of `pageLoadTime` is not known before the page load, so it's named normally. But it's still a constant because it doesn't change after the assignment.
301
301
302
302
In other words, capital-named constants are only used as aliases for "hard-coded" values.
303
303
@@ -307,18 +307,18 @@ Talking about variables, there's one more extremely important thing.
307
307
308
308
A variable name should have a clean, obvious meaning, describing the data that it stores.
309
309
310
-
Variable naming is one of the most important and complex skills in programming. A quick glance at variable names can reveal which code was written by a beginner versus an experienced developer.
310
+
Variable naming is one of the most important and complex skills in programming. A glance at variable names can reveal which code was written by a beginner versus an experienced developer.
311
311
312
-
In a real project, most of the time is spent modifying and extending an existing code base rather than writing something completely separate from scratch. When we return to some code after doing something else for a while, it's much easier to find information that is well-labeled. Or, in other words, when the variables have good names.
312
+
In a real project, most of the time is spent modifying and extending an existing code base rather than writing something completely separate from scratch. When we return to some code after doing something else for a while, it's much easier to find information that is well-labelled. Or, in other words, when the variables have good names.
313
313
314
314
Please spend time thinking about the right name for a variable before declaring it. Doing so will repay you handsomely.
315
315
316
316
Some good-to-follow rules are:
317
317
318
318
- Use human-readable names like `userName` or `shoppingCart`.
319
-
- Stay away from abbreviations or short names like `a`, `b`, `c`, unless you really know what you're doing.
319
+
- Stay away from abbreviations or short names like `a`, `b`, and `c`, unless you know what you're doing.
320
320
- Make names maximally descriptive and concise. Examples of bad names are `data` and `value`. Such names say nothing. It's only okay to use them if the context of the code makes it exceptionally obvious which data or value the variable is referencing.
321
-
- Agree on terms within your team and in your own mind. If a site visitor is called a "user" then we should name related variables `currentUser` or `newUser` instead of `currentVisitor` or `newManInTown`.
321
+
- Agree on terms within your team and in your mind. If a site visitor is called a "user" then we should name related variables `currentUser` or `newUser` instead of `currentVisitor` or `newManInTown`.
322
322
323
323
Sounds simple? Indeed it is, but creating descriptive and concise variable names in practice is not. Go for it.
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/06-polyfills/article.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ Teams behind JavaScript engines have their own ideas about what to implement fir
7
7
8
8
So it's quite common for an engine to implement only part of the standard.
9
9
10
-
A good page to see the current state of support for language features is <https://kangax.github.io/compat-table/es6/> (it's big, we have a lot to study yet).
10
+
A good page to see the current state of support for language features is <https://compat-table.github.io/compat-table/es6/> (it's big, we have a lot to study yet).
11
11
12
12
As programmers, we'd like to use most recent features. The more good stuff - the better!
13
13
@@ -85,7 +85,7 @@ Just don't forget to use a transpiler (if using modern syntax or operators) and
85
85
For example, later when you're familiar with JavaScript, you can setup a code build system based on [webpack](https://webpack.js.org/) with the [babel-loader](https://github.com/babel/babel-loader) plugin.
86
86
87
87
Good resources that show the current state of support for various features:
88
-
- <https://kangax.github.io/compat-table/es6/> - for pure JavaScript.
88
+
- <https://compat-table.github.io/compat-table/es6/> - for pure JavaScript.
89
89
- <https://caniuse.com/> - for browser-related functions.
90
90
91
91
P.S. Google Chrome is usually the most up-to-date with language features, try it if a tutorial demo fails. Most tutorial demos work with any modern browser though.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/article.md
+20-14Lines changed: 20 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ In modern JavaScript, there are two types of numbers:
4
4
5
5
1. Regular numbers in JavaScript are stored in 64-bit format [IEEE-754](https://en.wikipedia.org/wiki/IEEE_754), also known as "double precision floating point numbers". These are numbers that we're using most of the time, and we'll talk about them in this chapter.
6
6
7
-
2. BigInt numbers represent integers of arbitrary length. They are sometimes needed because a regular integer number can't safely exceed <code>(2<sup>53</sup>-1)</code> or be less than <code>-(2<sup>53</sup>-1)</code>, as we mentioned earlier in the chapter <info:types>. As bigints are used in few special areas, we devote them a special chapter <info:bigint>.
7
+
2. BigInt numbers represent integers of arbitrary length. They are sometimes needed because a regular integer number can't safely exceed <code>(2<sup>53</sup>-1)</code> or be less than <code>-(2<sup>53</sup>-1)</code>, as we mentioned earlier in the chapter <info:types>. As bigints are used in a few special areas, we devote them to a special chapter <info:bigint>.
8
8
9
9
So here we'll talk about regular numbers. Let's expand our knowledge of them.
10
10
@@ -41,7 +41,7 @@ In other words, `e` multiplies the number by `1` with the given zeroes count.
41
41
1.23e6===1.23*1000000; // e6 means *1000000
42
42
```
43
43
44
-
Now let's write something very small. Say, 1 microsecond (onemillionth of a second):
44
+
Now let's write something very small. Say, 1 microsecond (one-millionth of a second):
The `base` can vary from `2` to `36`. By default it's `10`.
106
+
The `base` can vary from `2` to `36`. By default, it's `10`.
107
107
108
108
Common use cases for this are:
109
109
110
110
-**base=16** is used for hex colors, character encodings etc, digits can be `0..9` or `A..F`.
111
111
-**base=2** is mostly for debugging bitwise operations, digits can be `0` or `1`.
112
-
-**base=36** is the maximum, digits can be `0..9` or `A..Z`. The whole latin alphabet is used to represent a number. A funny, but useful case for `36` is when we need to turn a long numeric identifier into something shorter, for example to make a short url. Can simply represent it in the numeral system with base `36`:
112
+
-**base=36** is the maximum, digits can be `0..9` or `A..Z`. The whole Latin alphabet is used to represent a number. A funny, but useful case for `36` is when we need to turn a long numeric identifier into something shorter, for example, to make a short url. Can simply represent it in the numeral system with base `36`:
113
113
114
114
```js run
115
115
alert( 123456..toString(36) ); // 2n9c
@@ -188,7 +188,7 @@ There are two ways to do so:
188
188
alert( num.toFixed(5) ); // "12.34000", added zeroes to make exactly 5 digits
189
189
```
190
190
191
-
We can convert it to a number using the unary plus or a `Number()` call, e.g write `+num.toFixed(5)`.
191
+
We can convert it to a number using the unary plus or a `Number()` call, e.g. write `+num.toFixed(5)`.
192
192
193
193
## Imprecise calculations
194
194
@@ -222,7 +222,13 @@ But why does this happen?
222
222
223
223
A number is stored in memory in its binary form, a sequence of bits - ones and zeroes. But fractions like `0.1`, `0.2` that look simple in the decimal numeric system are actually unending fractions in their binary form.
224
224
225
-
What is `0.1`? It is one divided by ten `1/10`, one-tenth. In decimal numeral system such numbers are easily representable. Compare it to one-third: `1/3`. It becomes an endless fraction `0.33333(3)`.
What is `0.1`? It is one divided by ten `1/10`, one-tenth. In the decimal numeral system, such numbers are easily representable. Compare it to one-third: `1/3`. It becomes an endless fraction `0.33333(3)`.
226
232
227
233
So, division by powers `10` is guaranteed to work well in the decimal system, but division by `3` is not. For the same reason, in the binary numeral system, the division by powers of `2` is guaranteed to work, but `1/10` becomes an endless binary fraction.
228
234
@@ -242,7 +248,7 @@ That's why `0.1 + 0.2` is not exactly `0.3`.
242
248
```smart header="Not only JavaScript"
243
249
The same issue exists in many other programming languages.
244
250
245
-
PHP, Java, C, Perl, Ruby give exactly the same result, because they are based on the same numeric format.
251
+
PHP, Java, C, Perl, and Ruby give exactly the same result, because they are based on the same numeric format.
246
252
```
247
253
248
254
Can we work around the problem? Sure, the most reliable method is to round the result with the help of a method [toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed):
So, multiply/divide approach reduces the error, but doesn't remove it totally.
275
+
So, the multiply/divide approach reduces the error, but doesn't remove it totally.
270
276
271
277
Sometimes we could try to evade fractions at all. Like if we're dealing with a shop, then we can store prices in cents instead of dollars. But what if we apply a discount of 30%? In practice, totally evading fractions is rarely possible. Just round them to cut "tails" when needed.
272
278
@@ -288,7 +294,7 @@ Another funny consequence of the internal representation of numbers is the exist
288
294
289
295
That's because a sign is represented by a single bit, so it can be set or not set for any number including a zero.
290
296
291
-
In most cases the distinction is unnoticeable, because operators are suited to treat them as the same.
297
+
In most cases, the distinction is unnoticeable, because operators are suited to treat them as the same.
292
298
```
293
299
294
300
## Tests: isFinite and isNaN
@@ -337,7 +343,7 @@ Please note that an empty or a space-only string is treated as `0` in all numeri
337
343
````smart header="`Number.isNaN` and `Number.isFinite`"
338
344
[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
345
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`.
346
+
- `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
347
342
348
```js run
343
349
alert( Number.isNaN(NaN) ); // true
@@ -348,7 +354,7 @@ Please note that an empty or a space-only string is treated as `0` in all numeri
348
354
alert( isNaN("str") ); // true, because isNaN converts string "str" into a number and gets NaN as a result of this conversion
349
355
```
350
356
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`.
357
+
- `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
358
353
359
```js run
354
360
alert( Number.isFinite(123) ); // true
@@ -367,7 +373,7 @@ In a way, `Number.isNaN` and `Number.isFinite` are simpler and more straightforw
367
373
There is a special built-in method `Object.is` that compares values like `===`, but is more reliable for two edge cases:
368
374
369
375
1. It works with `NaN`: `Object.is(NaN, NaN) === true`, that's a good thing.
370
-
2. Values `0` and `-0` are different: `Object.is(0, -0) === false`, technically that's correct, because internally the number has a sign bit that may be different even if all other bits are zeroes.
376
+
2. Values `0` and `-0` are different: `Object.is(0, -0) === false`, technically that's correct because internally the number has a sign bit that may be different even if all other bits are zeroes.
371
377
372
378
In all other cases, `Object.is(a, b)` is the same as `a === b`.
373
379
@@ -385,7 +391,7 @@ alert( +"100px" ); // NaN
385
391
386
392
The sole exception is spaces at the beginning or at the end of the string, as they are ignored.
387
393
388
-
But in real life we often have values in units, like `"100px"` or `"12pt"` in CSS. Also in many countries the currency symbol goes after the amount, so we have `"19€"` and would like to extract a numeric value out of that.
394
+
But in real life, we often have values in units, like `"100px"` or `"12pt"` in CSS. Also in many countries, the currency symbol goes after the amount, so we have `"19€"` and would like to extract a numeric value out of that.
389
395
390
396
That's what `parseInt` and `parseFloat` are for.
391
397
@@ -479,4 +485,4 @@ For fractions:
479
485
480
486
More mathematical functions:
481
487
482
-
- See the [Math](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math) object when you need them. The library is very small, but can cover basic needs.
488
+
- See the [Math](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math) object when you need them. The library is very small but can cover basic needs.
0 commit comments