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/05-data-types/04-array/article.md
+36-2Lines changed: 36 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,6 +92,38 @@ let fruits = [
92
92
The "trailing comma" style makes it easier to insert/remove items, because all lines become alike.
93
93
````
94
94
95
+
## Get last elements with "at"
96
+
97
+
[recent browser="new"]
98
+
99
+
Let's say we want a last element of the array.
100
+
101
+
Some programming languages allow to use negative indexes for the same purpose, like `fruits[-1]`.
102
+
103
+
Although, in JavaScript it won't work. The result will be `undefined`.
104
+
105
+
We can explicitly calculate the last element index and then access it, using `fruits[fruits.length - 1]`:
106
+
107
+
```js run
108
+
let fruits = ["Apple", "Orange", "Plum"];
109
+
110
+
alert( fruits[fruits.length-1] ); // Plum
111
+
```
112
+
113
+
A bit cumbersome, isn't it? We need to write the variable name twice.
114
+
115
+
Luckily, there's a shorter syntax: `fruits.at(-1)`:
116
+
117
+
```js run
118
+
let fruits = ["Apple", "Orange", "Plum"];
119
+
120
+
// same as fruits[fruits.length-1]
121
+
alert( fruits.at(-1) ); // Plum
122
+
```
123
+
124
+
In other words, `arr.at(i)`:
125
+
- is exactly the same as `arr[i]`, if `i >= 0`.
126
+
- for negative values of `i`, it steps back from the end of the array.
95
127
96
128
## Methods pop/push, shift/unshift
97
129
@@ -138,6 +170,8 @@ In computer science the data structure that allows this, is called [deque](https
138
170
alert( fruits ); // Apple, Orange
139
171
```
140
172
173
+
Both `fruits.pop()` and `fruits.at(-1)` return the last element of the array, but `fruits.pop()` also modifies the array by removing it.
174
+
141
175
`push`
142
176
: Append the element to the end of the array:
143
177
@@ -439,7 +473,7 @@ Let's recall the rules:
439
473
- If one of the arguments of `==` is an object, and the other one is a primitive, then the object gets converted to primitive, as explained in the chapter <info:object-toprimitive>.
440
474
- ...With an exception of `null` and `undefined` that equal `==` each other and nothing else.
441
475
442
-
The strict comparison `===` is even simpler, as it doesn't convert types.
476
+
The strict comparison `===` is even simpler, as it doesn't convert types.
443
477
444
478
So, if we compare arrays with `==`, they are never the same, unless we compare two variables that reference exactly the same array.
445
479
@@ -459,7 +493,7 @@ alert( 0 == [] ); // true
459
493
alert('0'== [] ); // false
460
494
```
461
495
462
-
Here, in both cases, we compare a primitive with an array object. So the array `[]` gets converted to primitive for the purpose of comparison and becomes an empty string `''`.
496
+
Here, in both cases, we compare a primitive with an array object. So the array `[]` gets converted to primitive for the purpose of comparison and becomes an empty string `''`.
463
497
464
498
Then the comparison process goes on with the primitives, as described in the chapter <info:type-conversions>:
0 commit comments