JavaScript Array at method

JavaScript Array at() method

Array.prototype.at() is a nice way to access elements in an array particularly the last.

Here’s our array:

const arr = [1,2,3,4,5];

New Array.prototype.at method:

// Getting the last element
arr.at(-1)  // Output: 5
 
// Getting element at index 2
arr.at(2)  // Output: 3

Comparison with traditional array access:

Here’s how the same operations would look using traditional array access:

// Getting the last element
arr[arr.length - 1]  // Output: 5
 
// Getting element at index 2
arr[2]  // Output: 3

The at() method provides a more elegant way to access elements, especially with the last element silliness that used to happen.

Browser support

Browser support is good https://caniuse.com/mdn-javascript_builtins_array_at