Array.prototype.at()
Returns the element at a given index, supporting negative indices.
Syntax
arr.at(index) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
index | number | Yes | The index to access. Negative values count back from the end of the array. |
Returns
any — The element at the given index, or undefined if out of range.
Examples
const a = [10, 20, 30];
console.log(a.at(-1));
Output
30
console.log([1, 2, 3].at(5));
Output
undefined
Notes
The main benefit over bracket access is clean negative indexing
(`arr.at(-1)` instead of `arr[arr.length - 1]`). Returns undefined for
out-of-range indices.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 92 |
| firefox | 90 |
| safari | 15.4 |
| edge | 92 |
| node | 16.6 |