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
Քանի որ `BigInt` թվերը հազվադեպ են օգտագործվում, դրանց այստեղ չենք անդրադառնա, այլ կդիտարկենք առանձին՝ <info:bigint> գլխում։ Կարդացե՛ք այն, եթե նման մեծ թվերի օգտագոծման կարիք կունենաք։
96
96
97
+
<<<<<<<HEAD
97
98
98
99
```smart header="Համատեղելիության խնդիրներ"
99
100
Այս պահին `BigInt`-ը համատեղելի է Firefox/Chrome/Edge/Safari զննիչների հետ, բայց ոչ IE-ի։
Կարող եք ստուգել [*MDN* BigInt compatibility table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) հոդվածը, հասկանալու համար, թե զննիչների որ տարբերակների հետ է այն համատեղելի։
103
104
104
105
## Տող (String)
106
+
=======
107
+
## String
108
+
>>>>>>>035c5267ba80fa7b55878f7213cbde449b4092d9
105
109
106
110
JavaScript-ում տողը պետք է շրջապատված լինի չակերտներով։
Copy file name to clipboardExpand all lines: 1-js/03-code-quality/06-polyfills/article.md
+1-4
Original file line number
Diff line number
Diff line change
@@ -71,10 +71,7 @@ if (!Math.trunc) { // if no such function
71
71
72
72
JavaScript is a highly dynamic language. Scripts may add/modify any function, even built-in ones.
73
73
74
-
Two interesting polyfill libraries are:
75
-
- [core js](https://github.com/zloirock/core-js) that supports a lot, allows to include only needed features.
76
-
- [polyfill.io](https://polyfill.io/) service that provides a script with polyfills, depending on the features and user's browser.
77
-
74
+
One interesting polyfill library is [core-js](https://github.com/zloirock/core-js), which supports a wide range of features and allows you to include only the ones you need.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/02-number/article.md
+24-16
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
@@ -118,7 +118,7 @@ Common use cases for this are:
118
118
```warn header="Two dots to call a method"
119
119
Please note that two dots in `123456..toString(36)` is not a typo. If we want to call a method directly on a number, like `toString` in the example above, then we need to place two dots `..` after it.
120
120
121
-
If we placed a single dot: `123456.toString(36)`, then there would be an error, because JavaScript syntax implies the decimal part after the first dot. And if we place one more dot, then JavaScript knows that the decimal part is empty and now goes the method.
121
+
If we placed a single dot: `123456.toString(36)`, then there would be an error, because JavaScript syntax implies the decimal part after the first dot. And if we place one more dot, then JavaScript knows that the decimal part is empty and now uses the method.
122
122
123
123
Also could write `(123456).toString(36)`.
124
124
@@ -137,7 +137,7 @@ There are several built-in functions for rounding:
137
137
: Rounds up:`3.1` becomes `4`, and `-1.1` becomes `-1`.
138
138
139
139
`Math.round`
140
-
: Rounds to the nearest integer:`3.1` becomes `3`, `3.6` becomes `4`, the middle case:`3.5` rounds up to `4` too.
140
+
: Rounds to the nearest integer:`3.1` becomes `3`, `3.6` becomes `4`. Inthe middle cases`3.5` rounds up to `4`, and `-3.5` rounds up to `-3`.
141
141
142
142
`Math.trunc` (not supported by Internet Explorer)
143
143
: Removes anything after the decimal point without rounding:`3.1` becomes `3`, `-1.1` becomes `-1`.
@@ -147,8 +147,10 @@ Here's the table to summarize the differences between them:
alert( num.toFixed(5) ); // "12.34000", added zeroes to make exactly 5 digits
189
191
```
190
192
191
-
We can convert it to a number using the unary plus or a `Number()` call, e.g write `+num.toFixed(5)`.
193
+
We can convert it to a number using the unary plus or a `Number()` call, e.g. write `+num.toFixed(5)`.
192
194
193
195
## Imprecise calculations
194
196
@@ -222,7 +224,13 @@ But why does this happen?
222
224
223
225
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
226
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
234
227
235
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
236
@@ -242,7 +250,7 @@ That's why `0.1 + 0.2` is not exactly `0.3`.
242
250
```smart header="Not only JavaScript"
243
251
The same issue exists in many other programming languages.
244
252
245
-
PHP, Java, C, Perl, Ruby give exactly the same result, because they are based on the same numeric format.
253
+
PHP, Java, C, Perl, and Ruby give exactly the same result, because they are based on the same numeric format.
246
254
```
247
255
248
256
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.
277
+
So, the multiply/divide approach reduces the error, but doesn't remove it totally.
270
278
271
279
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
280
@@ -288,7 +296,7 @@ Another funny consequence of the internal representation of numbers is the exist
288
296
289
297
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
298
291
-
In most cases the distinction is unnoticeable, because operators are suited to treat them as the same.
299
+
In most cases, the distinction is unnoticeable, because operators are suited to treat them as the same.
292
300
```
293
301
294
302
## Tests: isFinite and isNaN
@@ -337,7 +345,7 @@ Please note that an empty or a space-only string is treated as `0` in all numeri
337
345
````smart header="`Number.isNaN` and `Number.isFinite`"
338
346
[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
347
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`.
348
+
- `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
349
342
350
```js run
343
351
alert( Number.isNaN(NaN) ); // true
@@ -348,7 +356,7 @@ Please note that an empty or a space-only string is treated as `0` in all numeri
348
356
alert( isNaN("str") ); // true, because isNaN converts string "str" into a number and gets NaN as a result of this conversion
349
357
```
350
358
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`.
359
+
- `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
360
353
361
```js run
354
362
alert( Number.isFinite(123) ); // true
@@ -367,7 +375,7 @@ In a way, `Number.isNaN` and `Number.isFinite` are simpler and more straightforw
367
375
There is a special built-in method `Object.is` that compares values like `===`, but is more reliable for two edge cases:
368
376
369
377
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.
378
+
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
379
372
380
In all other cases, `Object.is(a, b)` is the same as `a === b`.
373
381
@@ -385,7 +393,7 @@ alert( +"100px" ); // NaN
385
393
386
394
The sole exception is spaces at the beginning or at the end of the string, as they are ignored.
387
395
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.
396
+
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
397
390
398
That's what `parseInt` and `parseFloat` are for.
391
399
@@ -479,4 +487,4 @@ For fractions:
479
487
480
488
More mathematical functions:
481
489
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.
490
+
- 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