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

ParameterDescription
index Position; negative values count back from the end.

Return value

The element, or undefined if the index is out of range.

Examples

Positive and negative indexesTry it yourself
const colors = ["red", "green", "blue"];
console.log(colors.at(0), colors.at(-1), colors.at(5));

Output

red blue undefined
The last elementTry it yourself
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

VersionChange
JavaScript ES2022Added