JavaScript Array.prototype.at() Method
The at() method returns the element at a given index, and accepts negative numbers to count back from the end of the array.
Description
The at() method of Array instances returns the element at an index. It works like square-bracket access, arr[i], with one difference: negative indexes count from the end, so arr.at(-1) is the last element and arr.at(-2) the one before it.
It is a shorter, clearer replacement for arr[arr.length - 1]. Indexes outside the array return undefined.
at() also exists on strings and typed arrays, so the same pattern works for all of them. Non-integer indexes are truncated, so at(1.7) is the same as at(1). Like bracket access, it doesn't change the array, and it's a read-only operation; to replace an element at a negative index without mutating the array, use with().
Syntax
array.at(index)Parameters
- indexThe position of the element. Negative numbers count back from the end, so
-1is the last element.
Return Value
The element at that position, or undefined if the index is out of range.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.at() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





