ES2022 introduced at() method to arrays, strings, and Typed Array.

Usually, Arrays always start with index=0 and retrieve the elements using a positive index. The negative index does not work before ES20201.

ES2022 .at() method

.at() method added to all indexable classes such as an array, string, and TypedArray.

For example, To get an element from an array, We always use index=0 to index=length -1. Before ES2022, the Positive index is supported and the negative index is not supported.

With this release, the Negative index supports indexable classes.

array.at[index]

the index is positive or negative.

the positive index is counted from the first element. index=0 in array returns the first element.

a negative index is a count from the last element in the array. index=-1 in array returns last element and equal to the array.length-1

Here is an example

const myarray = [5, 6, 13, 43, 35]

console.log(myarray.at(-1)) // 35
console.log(myarray[myarray.length - 1]) // 35

console.log(myarray.at(1)) // 5
console.log(myarray[1]) // 5
console.log(myarray.at(-1)) // 43
console.log(myarray.at(-2)) // 13

With ES13, Arrays and strings elements are getting using square bracket syntax[] or at() javascript method.

.at() method works with an array, string typed array in latest javascript.

How to get the last element of an array?

IN javascript you can get the last element using an index=array.length-1 since the array index starts from zero.

With ES2020, at() method is used to get elements from the negative index starting from the last element.

Here is an example

const myarray = ["one","two","three","four","five","six"]
// ES2022 syntax
console.log(myarray.at(-1)) // six

// Before ES2022
console.log(myarray[myarray.length - 1]) // six