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 javascript at() method

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

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

With this release, the Negative index supports indexable classes.

array.at[index]

an 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 the array returns the last element and is 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 the latest javascript?

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

With ES2022, the `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