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/05-types/article.md
+24-11Lines changed: 24 additions & 11 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
@@ -213,7 +224,7 @@ The `symbol` type is used to create unique identifiers for objects. We have to m
213
224
214
225
## The typeof operator [#type-typeof]
215
226
216
-
The `typeof` operator returns the type of the argument. It's useful when we want to process values of different types differently or just want to do a quick check.
227
+
The `typeof` operator returns the type of the operand. It's useful when we want to process values of different types differently or just want to do a quick check.
217
228
218
229
A call to `typeof x` returns a string with the type name:
219
230
@@ -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/08-operators/article.md
+16-15Lines changed: 16 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,8 +50,9 @@ The result of `a % b` is the [remainder](https://en.wikipedia.org/wiki/Remainder
50
50
For instance:
51
51
52
52
```js run
53
-
alert( 5 % 2 ); // 1, a remainder of 5 divided by 2
54
-
alert( 8 % 3 ); // 2, a remainder of 8 divided by 3
53
+
alert( 5 % 2 ); // 1, the remainder of 5 divided by 2
54
+
alert( 8 % 3 ); // 2, the remainder of 8 divided by 3
55
+
alert( 8 % 4 ); // 0, the remainder of 8 divided by 4
55
56
```
56
57
57
58
### Exponentiation **
@@ -68,7 +69,7 @@ alert( 2 ** 3 ); // 2³ = 8
68
69
alert( 2 ** 4 ); // 2⁴ = 16
69
70
```
70
71
71
-
Just like in maths, the exponentiation operator is defined for non-integer numbers as well.
72
+
Just like in maths, the exponentiation operator is defined for non-integer numbers as well.
72
73
73
74
For example, a square root is an exponentiation by ½:
74
75
@@ -80,7 +81,7 @@ alert( 8 ** (1/3) ); // 2 (power of 1/3 is the same as a cubic root)
80
81
81
82
## String concatenation with binary +
82
83
83
-
Let's meet features of JavaScript operators that are beyond school arithmetics.
84
+
Let's meet the features of JavaScript operators that are beyond school arithmetics.
84
85
85
86
Usually, the plus operator `+` sums numbers.
86
87
@@ -194,18 +195,18 @@ Here's an extract from the [precedence table](https://developer.mozilla.org/en-U
194
195
| Precedence | Name | Sign |
195
196
|------------|------|------|
196
197
|...|...|...|
197
-
|15| unary plus |`+`|
198
-
|15| unary negation |`-`|
199
-
|14| exponentiation |`**`|
200
-
|13| multiplication |`*`|
201
-
|13| division |`/`|
202
-
|12| addition |`+`|
203
-
|12| subtraction |`-`|
198
+
|14| unary plus |`+`|
199
+
|14| unary negation |`-`|
200
+
|13| exponentiation |`**`|
201
+
|12| multiplication |`*`|
202
+
|12| division |`/`|
203
+
|11| addition |`+`|
204
+
|11| subtraction |`-`|
204
205
|...|...|...|
205
206
|2| assignment |`=`|
206
207
|...|...|...|
207
208
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.
209
+
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
210
210
211
## Assignment
211
212
@@ -303,9 +304,9 @@ Such operators have the same precedence as a normal assignment, so they run afte
303
304
```js run
304
305
let n = 2;
305
306
306
-
n *= 3 + 5;
307
+
n *= 3 + 5; // right part evaluated first, same as n *= 8
307
308
308
-
alert( n ); // 16 (right part evaluated first, same as n *= 8)
309
+
alert( n ); // 16
309
310
```
310
311
311
312
## Increment/decrement
@@ -437,7 +438,7 @@ The list of operators:
437
438
- RIGHT SHIFT ( `>>` )
438
439
- ZERO-FILL RIGHT SHIFT ( `>>>` )
439
440
440
-
These operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won't need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the [Bitwise Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Bitwise) chapter on MDN when a need arises.
441
+
These operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won't need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the [Bitwise Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#bitwise_operators) chapter on MDN when a need arises.
0 commit comments