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
@@ -233,6 +233,37 @@ Most of time, the dot is used to access object properties, but when we need a co
233
233
234
234
Objects in JavaScript are very powerful. Here we've just scratched the surface of the topic that is really huge. We'll be closely working with objects and learning more about them in further parts of the tutorial.
235
235
236
+
## Arrays
237
+
238
+
As we’ve just seen, objects in Javascript store arbitrary keyed values.
239
+
240
+
But quite often we find that we need an *ordered collection*, where we have a 1st, a 2nd, a 3rd element and so on. For example, we need that to store a list of something: users, goods, HTML elements etc. It not convenient to use an object here, because it provides no methods to manage the order ofelements. We can’t easily access the n-th element in an object. Also we can’t insert a newproperty “before” the existing ones, and so on. Objects are just not meant for such use.
241
+
242
+
There exists a special data structure named "an array", to store ordered collections.
243
+
244
+
An array is created using square brackets:
245
+
246
+
```js
247
+
let empty = []; // empty array
248
+
249
+
let fruits = ["Apple", "Orange", "Plum"]; // array with 3 values
250
+
```
251
+
252
+
Individual items are accessed using brackets `[]`, the first item has zero index:
253
+
254
+
```js run
255
+
let fruits = ["Apple", "Orange", "Plum"]; // array with 3 values
256
+
257
+
alert( fruits[0] ); // Apple
258
+
alert( fruits[1] ); // Orange
259
+
alert( fruits[2] ); // Plum
260
+
261
+
// how many elements (last index + 1)
262
+
alert( fruits.length ); // 3
263
+
```
264
+
265
+
Please note that arrays do not form a separate language type. They are based on objects, but have many features of their own including methods to add, remove, extract elements from the array, to sort arrays and more. We'll cover them in the chapter <info:array>.
266
+
236
267
## Symbol type
237
268
238
269
The `symbol` type is used in conjunction with objects. Probably we won't need them any time soon, but it's the 7th and the last type of the language, so we must mention it here for the sake of completeness.
Please note the last two lines, because `typeof` behaves a little bit strange there.
331
-
332
-
1. The result of `typeof null` equals to `"object"`. That is an officially recognized error in `typeof` implementation, kept for compatibility. Of course, `null` is not an object. It is a special value with a separate type of its own. So `typeof` just says it wrong here.
333
-
2. The result of `typeof alert` is `"function"`, because `alert` is a function of the language. We'll study functions in the near future and see that actually functions belong to the object type. But`typeof` treats them differently. That's very convenient in practice.
334
-
335
-
336
-
## Type conversions
337
-
338
-
A variable in JavaScript can contain any data. A variable can at one moment be a string and later recieve a numeric value:
339
-
340
-
```js
341
-
// no error
342
-
let message = "hello";
343
-
message = 123456;
344
-
```
345
-
346
-
...But sometimes we need to convert a value from one type to another. For example, `alert` automatically converts any value to a string, to show it. Or, so to say, an `if(value)` test converts the `value` to boolean type to see if it's `true` or `false`.
347
-
348
-
There are also cases when we need to explicitly convert between types to ensure that we store the right data the right way or to use special features of a certain type.
349
-
350
-
There are many type conversions in JavaScript, fully listed in [the specification](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-type-conversion).
351
-
352
-
Three conversions that happen the most often:
353
-
354
-
1.String conversion.
355
-
2. Numeric conversion.
356
-
3.Boolean conversion.
357
-
358
-
Let's see how they work and when they happen.
359
-
360
-
### String conversion
361
-
362
-
The string conversion happens when we need a string form of a value.
363
-
364
-
For example, `alert` does it:
365
-
366
-
```js run
367
-
let a = true;
368
-
369
-
alert( a ); // "true"
370
-
```
371
-
372
-
We can also use a call `String(value)` function for that:
373
-
374
-
```js run
375
-
let a = true;
376
-
alert(typeof a); // boolean
377
359
378
360
*!*
379
-
a = String(a); // now: a = "true"
380
-
alert(typeof a); // string
361
+
typeof alert // "function" (3)
381
362
*/!*
382
363
```
383
364
384
-
The string conversion is obvious. A `false` becomes `"false"`, `null` becomes `"null"` etc.
385
-
386
-
### Numeric conversion
387
-
388
-
Numeric conversion happens in mathematical functions and expressions automatically.
389
-
390
-
For example, a mathematical operation like division '/' can be applied to non-numbers:
391
-
392
-
```js run
393
-
alert( "6" / "2" ); // 3, strings become numbers
394
-
```
395
-
396
-
We can use a `Number(value)` function to convert any `value` to a number:
397
-
398
-
```js run
399
-
let str = "123";
400
-
alert(typeof str); // string
401
-
402
-
let n = Number(str); // becomes a number 123
403
-
404
-
alert(typeof n); // number
405
-
```
406
-
407
-
The conversion is usually applied when we have a numeric value coming from a text form field or another string-based source.
408
-
409
-
If the string is not a number, the result of such conversion is `NaN`, for instance:
410
-
411
-
```js run
412
-
let age = Number("an arbitrary string instead of a number");
413
-
414
-
alert(age); // NaN, conversion failed
415
-
```
416
-
417
-
The numeric conversion rules:
418
-
419
-
| Value | Becomes... |
420
-
|-------|-------------|
421
-
|`undefined`|`NaN`|
422
-
|`null`|`0`|
423
-
|<code>true / false</code> | `1 / 0` |
424
-
| `string` | Whitespaces from the start and the end are removed. Then, if the remaining string is empty, the result is `0`, otherwise –- the number is "read" from the string. An error gives `NaN`. |
425
-
426
-
Examples:
427
-
428
-
```js run
429
-
alert( Number(" 123 ") ); // 123
430
-
alert( Number("123z") ); // NaN (error reading a number at "z")
431
-
alert( Number(true) ); // 1
432
-
alert( Number(false) ); // 0
433
-
```
434
-
435
-
Please note that `null` and `undefined` behave differently here: `null` becomes a zero, while `undefined` becomes `NaN`.
436
-
437
-
### Boolean conversion
438
-
439
-
Boolean conversion is the simplest one.
440
-
441
-
It happens in logical operations (later we'll meet `if` tests and other kinds), but also can be performed manually with the call of`Boolean(value)`.
365
+
Please note the last lines.
442
366
443
-
The conversion rule:
367
+
1. The array is not a type of its own, but a subtype of object, that's why `typeof []` is `"object"`.
368
+
2. The result of `typeof null` equals to `"object"`. That's wrong. It is an officially recognized error in`typeof` implementation, kept forcompatibility. Of course, `null` is not an object. It is a special value with a separate type of its own.
369
+
3. The result of`typeof alert` is `"function"`, because `alert` is a function of the language. We'll study functions in the near future and see that actually functions belong to the object type. But `typeof` treats them differently. That's very convenient in practice.
444
370
445
-
- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined` and `NaN` become `false`.
446
-
- Other values become `true`.
447
-
448
-
````warn header="Please note: a string `\"0\"` is `true`"
449
-
Some languages (namely PHP) treat `"0"` as `false`. Butin JavaScript a non-empty string is always `false`, no matter what is in it.
450
-
451
-
```js run
452
-
alert( Boolean("0") ); // true
453
-
alert( Boolean(" ") ); // any non-empty string, even whitespaces are true
454
-
```
455
-
````
456
371
457
372
458
373
## Summary
@@ -464,9 +379,8 @@ There are 7 basic types in JavaScript.
464
379
- `boolean` for `true`/`false`, can convert into it using `Boolean(value)`.
465
380
- `null` for unknown values.
466
381
- `undefined` for unassigned values.
467
-
-`object`for complex data structures.
468
382
- `symbol` for unique identifiers.
383
+
- `object` for more complex data structures (there exist many, we saw arrays).
469
384
470
-
The `typeof` operator allows to see which type is stored in the variable, but note that it mistakingly returns `"object"`for`null`.
385
+
The `typeof` operator allows to see which type is stored in the variable.
471
386
472
-
Now let's study operators and other language constructs that actually form our code.
A variable in JavaScript can contain any data. A variable can at one moment be a string and later recieve a numeric value:
4
+
5
+
```js
6
+
// no error
7
+
let message ="hello";
8
+
message =123456;
9
+
```
10
+
11
+
...But sometimes we need to convert a value from one type to another. For example, `alert` automatically converts any value to a string, to show it. Or, so to say, an `if(value)` test converts the `value` to boolean type to see if it's `true` or `false`.
12
+
13
+
There are also cases when we need to explicitly convert between types to ensure that we store the right data the right way or to use special features of a certain type.
14
+
15
+
There are many type conversions in JavaScript, fully listed in [the specification](http://www.ecma-international.org/ecma-262/6.0/index.html#sec-type-conversion).
16
+
17
+
Three conversions that happen the most often:
18
+
19
+
1. String conversion.
20
+
2. Numeric conversion.
21
+
3. Boolean conversion.
22
+
23
+
Let's see how they work and when they happen.
24
+
25
+
## String conversion
26
+
27
+
The string conversion happens when we need a string form of a value.
28
+
29
+
For example, `alert` does it:
30
+
31
+
```js run
32
+
let a =true;
33
+
34
+
alert( a ); // "true"
35
+
```
36
+
37
+
We can also use a call `String(value)` function for that:
38
+
39
+
```js run
40
+
let a =true;
41
+
alert(typeof a); // boolean
42
+
43
+
*!*
44
+
a =String(a); // now: a = "true"
45
+
alert(typeof a); // string
46
+
*/!*
47
+
```
48
+
49
+
The string conversion is obvious. A `false` becomes `"false"`, `null` becomes `"null"` etc.
50
+
51
+
## Numeric conversion
52
+
53
+
Numeric conversion happens in mathematical functions and expressions automatically.
54
+
55
+
For example, a mathematical operation like division '/' can be applied to non-numbers:
56
+
57
+
```js run
58
+
alert( "6"/"2" ); // 3, strings become numbers
59
+
```
60
+
61
+
We can use a `Number(value)` function to convert any `value` to a number:
62
+
63
+
```js run
64
+
let str ="123";
65
+
alert(typeof str); // string
66
+
67
+
let n =Number(str); // becomes a number 123
68
+
69
+
alert(typeof n); // number
70
+
```
71
+
72
+
The conversion is usually applied when we have a numeric value coming from a text form field or another string-based source.
73
+
74
+
If the string is not a number, the result of such conversion is `NaN`, for instance:
75
+
76
+
```js run
77
+
let age =Number("an arbitrary string instead of a number");
78
+
79
+
alert(age); // NaN, conversion failed
80
+
```
81
+
82
+
The numeric conversion rules:
83
+
84
+
| Value | Becomes... |
85
+
|-------|-------------|
86
+
|`undefined`|`NaN`|
87
+
|`null`|`0`|
88
+
|<code>true and false</code> |`1` and `0`|
89
+
|`string`| Whitespaces from the start and the end are removed. Then, if the remaining string is empty, the result is `0`, otherwise -- the number is "read" from the string. An error gives `NaN`. |
90
+
91
+
Examples:
92
+
93
+
```js run
94
+
alert( Number(" 123 ") ); // 123
95
+
alert( Number("123z") ); // NaN (error reading a number at "z")
96
+
alert( Number(true) ); // 1
97
+
alert( Number(false) ); // 0
98
+
```
99
+
100
+
Please note that `null` and `undefined` behave differently here: `null` becomes a zero, while `undefined` becomes `NaN`.
101
+
102
+
## Boolean conversion
103
+
104
+
Boolean conversion is the simplest one.
105
+
106
+
It happens in logical operations (later we'll meet `if` tests and other kinds), but also can be performed manually with the call of `Boolean(value)`.
107
+
108
+
The conversion rule:
109
+
110
+
- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined` and `NaN` become `false`.
111
+
- Other values become `true`.
112
+
113
+
````warn header="Please note: the string with zero `\"0\"` is `true`"
114
+
Some languages (namely PHP) treat `"0"` as `false`. But in JavaScript a non-empty string is always `true`.
115
+
116
+
```js run
117
+
alert( Boolean("0") ); // true
118
+
alert( Boolean("") ); // any non-empty string, even whitespaces are true
119
+
```
120
+
````
121
+
122
+
## Summary
123
+
124
+
There exist three most widely used type conversions: to string, to number and to boolean.
125
+
126
+
The conversion to string is usully obvious.
127
+
128
+
To number follows the rules:
129
+
130
+
| Value | Becomes... |
131
+
|-------|-------------|
132
+
|`undefined`|`NaN`|
133
+
|`null`|`0`|
134
+
|<code>true / false</code> | `1 / 0` |
135
+
| `string` | The string is read "as is", whitespaces from both sides are ignored. An empty string is `0`. An error gives `NaN`. |
136
+
137
+
To boolean:
138
+
139
+
| Value | Becomes... |
140
+
|-------|-------------|
141
+
|`0`, `null`, `undefined`, `NaN`, `""` |`false`|
142
+
|any other value| `true` |
143
+
144
+
Objects provide advanced means to specify how they are converted to string and number, we'll see them later, when study objects in-depth.
145
+
146
+
Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are:
147
+
148
+
- `undefined` is `NaN` as a number.
149
+
- `"0"` is true as a boolean.
150
+
151
+
In the next chapter we'll study operators. You will find enough tasks for type conversion there.
0 commit comments