JavaScript (ES2026) reference · Array Methods
JavaScript Array at() Method
Return the element at an index, where negative indexes count from the end: arr.at(-1) is the last element.
JavaScript (ES2026) Since ES2022
Syntax
array.at(index)
Parameters
| Parameter | Description |
|---|---|
index |
Position; negative values count back from the end. |
Return value
The element, or undefined if the index is out of range.
Examples
const colors = ["red", "green", "blue"];
console.log(colors.at(0), colors.at(-1), colors.at(5));
Output
red blue undefined
const scores = [70, 85, 92];
console.log(scores[scores.length - 1]); // the old way
console.log(scores.at(-1)); // the same, shorter
Output
92 92
Version history
| Version | Change |
|---|---|
| JavaScript ES2022 | Added |
Related
array.indexOf()· Return the first index at which a value is found (using ===), or -1 if it isn't in the array.array.slice()· Return a shallow copy of part of the array, from start up to (not including) end. The original is not changed.array.with()· Return a copy of the array with one element replaced, leaving the original unchanged. Negative indexes count from the end.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial