Skip to content

Commit 61f79f4

Browse files
committed
at method
1 parent 58ef96a commit 61f79f4

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

1-js/05-data-types/04-array/article.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ let fruits = [
9292
The "trailing comma" style makes it easier to insert/remove items, because all lines become alike.
9393
````
9494

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.
95127

96128
## Methods pop/push, shift/unshift
97129

@@ -138,6 +170,8 @@ In computer science the data structure that allows this, is called [deque](https
138170
alert( fruits ); // Apple, Orange
139171
```
140172

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+
141175
`push`
142176
: Append the element to the end of the array:
143177

@@ -439,7 +473,7 @@ Let's recall the rules:
439473
- 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>.
440474
- ...With an exception of `null` and `undefined` that equal `==` each other and nothing else.
441475

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.
443477

444478
So, if we compare arrays with `==`, they are never the same, unless we compare two variables that reference exactly the same array.
445479

@@ -459,7 +493,7 @@ alert( 0 == [] ); // true
459493
alert('0' == [] ); // false
460494
```
461495

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 `''`.
463497

464498
Then the comparison process goes on with the primitives, as described in the chapter <info:type-conversions>:
465499

0 commit comments

Comments
 (0)