Array.prototype.at()
at(index) Returns:
any · Added in vES2022 · Updated March 13, 2026 · Array Methods array at indexing es2022
The at() method returns the element at the specified index in an array. What makes it special is that it accepts negative indices to count from the end of the array — something traditional bracket notation [Global_Objects::eval] cannot do.
Syntax
array.at(index)
index: The index of the element to return. Can be positive (from start) or negative (from end).- Returns: The element at the specified index, or
undefinedif the index is out of bounds.
TypeScript Signature
interface Array<T> {
at(index: number): T | undefined;
}
Basic Examples
const fruits = ["apple", "banana", "cherry", "date"];
// Positive index - same as bracket notation
console.log(fruits.at(0)); // "apple"
console.log(fruits.at(2)); // "cherry"
// Negative index - count from end
console.log(fruits.at(-1)); // "date" (last element)
console.log(fruits.at(-2)); // "cherry"
console.log(fruits.at(-4)); // "apple"
// Out of bounds returns undefined
console.log(fruits.at(10)); // undefined
console.log(fruits.at(-10)); // undefined
Why Use at() Instead of Bracket Notation?
Before ES2022, getting the last element required arr[arr.length - 1]. With at(), simply use -1:
const items = [1, 2, 3];
// Old way
console.log(items[items.length - 1]); // 3
// New way with at()
console.log(items.at(-1)); // 3
This is especially useful when:
- The array length is unknown or dynamic
- Chaining methods without storing array length
- Writing readable code for “last element” access
- Getting second-to-last, third-to-last elements easily
Practical Examples
Getting the Last Element
function getLastItem(arr) {
return arr.at(-1);
}
console.log(getLastItem([1, 2, 3])); // 3
console.log(getLastItem(["a", "b", "c"])); // "c"
console.log(getLastItem([Global_Objects::eval])); // undefined
Getting Second-to-Last Element
const scores = [95, 87, 92, 88, 79];
console.log(scores.at(-2)); // 88 (second highest)
Chaining with Other Array Methods
const data = [10, 20, 30, 40, 50];
// Get last element after filtering
const result = data.filter(x => x > 25).at(-1);
console.log(result); // 50
// Get last element after mapping
const lastDoubled = data.map(x => x * 2).at(-1);
console.log(lastDoubled); // 100
Edge Cases
const arr = ["first"];
// Empty result from filter
console.log([Global_Objects::eval].at(-1)); // undefined
// Works with sparse arrays
const sparse = [1, , 3];
console.log(sparse.at(1)); // undefined (not 3)
// Negative index larger than length
console.log(arr.at(-5)); // undefined
Browser Compatibility
| Browser | Version |
|---|---|
| Chrome | 92+ |
| Firefox | 90+ |
| Safari | 15.4+ |
| Edge | 92+ |
Polyfill
Legacy environments can use this polyfill:
if (!Arrayat) {
Arrayat = function(n) {
n = Math.trunc(n) || 0;
if (n < 0) n += this.length;
return n >= 0 && n < this.length ? this[n] : undefined;
};
}
This polyfill handles negative indices and boundary checks just like the native implementation, ensuring consistent behavior across older JavaScript environments.
See Also
- string::at — Access characters by index (including negative)
- array::indexOf — Find element index by value