Array

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

JavaScript
array.at(index)

Parameters

ParameterTypeDescription
indexnumberZero-based index. Negative values count from the end

Return Value

The element at the given index, or undefined

Examples

Basic Usage
const arr = [5, 12, 8, 130, 44];
console.log(arr.at(2)); // 8
console.log(arr.at(-1)); // 44
Practical Example
const colors = ['red', 'green', 'blue'];
console.log(colors.at(-2)); // 'green'
Advanced Usage
function last<T>(arr: T[]): T | undefined {
  return arr.at(-1);
}
console.log(last([1, 2, 3])); // 3

Understanding 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

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.