Array.prototype.at
Takes an integer value and returns the item at that index, allowing for positive and negative integers where negative integers count back from the last item
Syntax
array.at(index)Parameters
| Parameter | Type | Description |
|---|---|---|
| index | number | Zero-based index. Negative values count from the end |
Return Value
The element at the given index, or undefined
Examples
const arr = [5, 12, 8, 130, 44];
console.log(arr.at(2)); // 8
console.log(arr.at(-1)); // 44const colors = ['red', 'green', 'blue'];
console.log(colors.at(-2)); // 'green'function last<T>(arr: T[]): T | undefined {
return arr.at(-1);
}
console.log(last([1, 2, 3])); // 3Understanding Array.prototype.at
The Array.prototype.at method in JavaScript takes an integer value and returns the item at that index, allowing for positive and negative integers where negative integers count back from the last item. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.
The method signature is array.at(index). It accepts 1 parameter: index. When called, it returns the element at the given index, or undefined. Understanding when and how to use at() helps you write more expressive, readable code.
Common use cases for Array.prototype.at include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-slice, array-find, string-at, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Array.prototype.at is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Array.prototype.sliceReturns a shallow copy of a portion of an array into a new array object selected from start to end (end not included)
Array.prototype.findReturns the first element in the provided array that satisfies the provided testing function
String.prototype.atTakes an integer value and returns the character at that index, supporting positive and negative integers
More Array Methods
Other methods in the Array object
Related Tools
More Array Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.