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
In the absence of `Symbol.toPrimitive` and `valueOf`, `toString` will handle all primitive conversions.
157
157
158
-
159
-
## ToPrimitive and ToString/ToNumber
158
+
## Return types
160
159
161
160
The important thing to know about all primitive-conversion methods is that they do not necessarily return the "hinted" primitive.
162
161
163
162
There is no control whether `toString()` returns exactly a string, or whether `Symbol.toPrimitive` method returns a number for a hint "number".
164
163
165
-
**The only mandatory thing: these methods must return a primitive.**
164
+
The only mandatory thing: these methods must return a primitive, not an object.
165
+
166
+
```smart header="Historical notes"
167
+
For historical reasons, if `toString` or `valueOf` return an object, there's no error, but such value is ignored (like if the method didn't exist). That's because in ancient times there was no good "error" concept in JavaScript.
168
+
169
+
In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise there will be an error.
170
+
```
171
+
172
+
## Further operations
166
173
167
174
An operation that initiated the conversion gets that primitive, and then continues to work with it, applying further conversions if necessary.
168
175
@@ -204,11 +211,6 @@ For instance:
204
211
alert(obj + 2); // 3 (ToPrimitive returned boolean, not string => ToNumber)
205
212
```
206
213
207
-
```smart header="Historical notes"
208
-
For historical reasons, methods `toString` or `valueOf` *should* return a primitive: if any of them returns an object, then there's no error, but that object is ignored (like if the method didn't exist).
209
-
210
-
In contrast, `Symbol.toPrimitive` *must* return a primitive, otherwise, there will be an error.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/04-array/article.md
+17-18
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,12 @@
1
-
# Arrays
1
+
# Arrays
2
2
3
3
Objects allow you to store keyed collections of values. That's fine.
4
4
5
-
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.
5
+
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.
6
6
7
7
It is not convenient to use an object here, because it provides no methods to manage the order of elements. We can’t insert a new property “between” the existing ones. Objects are just not meant for such use.
8
8
9
-
There exists a special data structure named `Array`, to store ordered collections.
9
+
There exists a special data structure named `Array`, to store ordered collections.
10
10
11
11
## Declaration
12
12
@@ -81,10 +81,10 @@ arr[3](); // hello
81
81
82
82
````smart header="Trailing comma"
83
83
An array, just like an object, may end with a comma:
84
-
```js
84
+
```js
85
85
let fruits = [
86
-
"Apple",
87
-
"Orange",
86
+
"Apple",
87
+
"Orange",
88
88
"Plum"*!*,*/!*
89
89
];
90
90
```
@@ -106,7 +106,7 @@ Arrays support both operations.
106
106
107
107
In practice we need it very often. For example, a queue of messages that need to be shown on-screen.
108
108
109
-
There's another use case for arrays -- the data structure named [stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)).
109
+
There's another use case for arrays -- the data structure named [stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)).
110
110
111
111
It supports two operations:
112
112
@@ -121,7 +121,7 @@ A stack is usually illustrated as a pack of cards: new cards are added to the to
121
121
122
122
For stacks, the latest pushed item is received first, that's also called LIFO (Last-In-First-Out) principle. For queues, we have FIFO (First-In-First-Out).
123
123
124
-
Arrays in JavaScript can work both as a queue and as a stack. They allow you to add/remove elements both to/from the beginning or the end.
124
+
Arrays in JavaScript can work both as a queue and as a stack. They allow you to add/remove elements both to/from the beginning or the end.
125
125
126
126
In computer science the data structure that allows it is called [deque](https://en.wikipedia.org/wiki/Double-ended_queue).
127
127
@@ -189,11 +189,11 @@ alert( fruits );
189
189
190
190
## Internals
191
191
192
-
An array is a special kind of object. The square brackets used to access a property `arr[0]` actually come from the object syntax. Numbers are used as keys.
192
+
An array is a special kind of object. The square brackets used to access a property `arr[0]` actually come from the object syntax. That's essentially the same as `obj[key]`, where `arr` is the object, while numbers are used as keys.
193
193
194
194
They extend objects providing special methods to work with ordered collections of data and also the `length` property. But at the core it's still an object.
195
195
196
-
Remember, there are only 7 basic types in JavaScript. Array is an object and thus behaves like an object.
196
+
Remember, there are only 7 basic types in JavaScript. Array is an object and thus behaves like an object.
197
197
198
198
For instance, it is copied by reference:
199
199
@@ -203,7 +203,7 @@ let fruits = ["Banana"]
203
203
let arr = fruits; // copy by reference (two variables reference the same array)
204
204
205
205
alert( arr === fruits ); // true
206
-
206
+
207
207
arr.push("Pear"); // modify the array by reference
208
208
209
209
alert( fruits ); // Banana, Pear - 2 items now
@@ -229,7 +229,7 @@ But the engine will see that we're working with the array as with a regular obje
229
229
230
230
The ways to misuse an array:
231
231
232
-
- Add a non-numeric property like `arr.test = 5`.
232
+
- Add a non-numeric property like `arr.test = 5`.
233
233
- Make holes, like: add `arr[0]` and then `arr[1000]` (and nothing between them).
234
234
- Fill the array in the reverse order, like `arr[1000]`, `arr[999]` and so on.
235
235
@@ -296,7 +296,7 @@ let fruits = ["Apple", "Orange", "Plum"];
296
296
297
297
// iterates over array elements
298
298
for (let fruit of fruits) {
299
-
alert( fruit );
299
+
alert( fruit );
300
300
}
301
301
```
302
302
@@ -320,7 +320,7 @@ But that's actually a bad idea. There are potential problems with it:
320
320
321
321
There are so-called "array-like" objects in the browser and in other environments, that *look like arrays*. That is, they have `length` and indexes properties, but they may also have other non-numeric properties and methods, which we usually don't need. The `for..in` loop will list them though. So if we need to work with array-like objects, then these "extra" properties can become a problem.
322
322
323
-
2. The `for..in` loop is optimized for generic objects, not arrays, and thus is 10-100 times slower. Of course, it's still very fast. The speedup may only matter in bottlenecks or seem irrelevant. But still we should be aware of the difference.
323
+
2. The `for..in` loop is optimized for generic objects, not arrays, and thus is 10-100 times slower. Of course, it's still very fast. The speedup may only matter in bottlenecks. But still we should be aware of the difference.
324
324
325
325
Generally, we shouldn't use `for..in` for arrays.
326
326
@@ -338,7 +338,7 @@ fruits[123] = "Apple";
338
338
alert( fruits.length ); // 124
339
339
```
340
340
341
-
Note that we usually don't use arrays like that.
341
+
Note that we usually don't use arrays like that.
342
342
343
343
Another interesting thing about the `length` property is that it's writable.
344
344
@@ -385,7 +385,7 @@ To evade such surprises, we usually use square brackets, unless we really know w
385
385
386
386
## Multidimensional arrays
387
387
388
-
Arrays can have items that are also arrays. We can use it for multidimensional arrays, to store matrices:
388
+
Arrays can have items that are also arrays. We can use it for multidimensional arrays, for example to store matrices:
389
389
390
390
```js run
391
391
let matrix = [
@@ -445,7 +445,7 @@ Array is a special kind of object, suited to storing and managing ordered data i
445
445
446
446
The call to `new Array(number)` creates an array with the given length, but without elements.
447
447
448
-
- The `length` property is the array length or, to be precise, its last numeric index plus one. It is auto-adjusted by array methods.
448
+
- The `length` property is the array length or, to be precise, its last numeric index plus one. It is auto-adjusted by array methods.
449
449
- If we shorten `length` manually, the array is truncated.
450
450
451
451
We can use an array as a deque with the following operations:
@@ -461,4 +461,3 @@ To loop over the elements of the array:
461
461
-`for (let i in arr)`-- never use.
462
462
463
463
We will return to arrays and study more methods to add, remove, extract elements and sort arrays in the chapter <info:array-methods>.
0 commit comments