Array.prototype.at()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.
Theat()
method ofArray
instances takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.
Try it
const array1 = [5, 12, 8, 130, 44];let index = 2;console.log(`An index of ${index} returns ${array1.at(index)}`);// Expected output: "An index of 2 returns 8"index = -2;console.log(`An index of ${index} returns ${array1.at(index)}`);// Expected output: "An index of -2 returns 130"
Syntax
at(index)
Parameters
index
Zero-based index of the array element to be returned,converted to an integer. Negative index counts back from the end of the array — if
index < 0
,index + array.length
is accessed.
Return value
The element in the array matching the given index. Always returnsundefined
ifindex < -array.length
orindex >= array.length
without attempting to access the corresponding property.
Description
Theat()
method is equivalent to the bracket notation whenindex
is a non-negative integer. For example,array[0]
andarray.at(0)
both return the first item. However, when counting elements from the end of the array, you cannot usearray[-1]
like you may in Python or R, because all values inside the square brackets are treated literally as string properties, so you will end up readingarray["-1"]
, which is just a normal string property instead of an array index.
The usual practice is to accesslength
and calculate the index from that — for example,array[array.length - 1]
. Theat()
method allows relative indexing, so this can be shortened toarray.at(-1)
.
By combiningat()
withwith()
, you can both read and write (respectively) an array using negative indices.
Theat()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties.
Examples
Return the last value of an array
The following example provides a function which returns the last element found in a specified array.
// Our array with itemsconst cart = ["apple", "banana", "pear"];// A function which returns the last item of a given arrayfunction returnLast(arr) { return arr.at(-1);}// Get the last item of our array 'cart'const item1 = returnLast(cart);console.log(item1); // 'pear'// Add an item to our 'cart' arraycart.push("orange");const item2 = returnLast(cart);console.log(item2); // 'orange'
Comparing methods
This example compares different ways to select the penultimate (last but one) item of anArray
. While all the methods shown below are valid, this example highlights the succinctness and readability of theat()
method.
// Our array with itemsconst colors = ["red", "green", "blue"];// Using length propertyconst lengthWay = colors[colors.length - 2];console.log(lengthWay); // 'green'// Using slice() method. Note an array is returnedconst sliceWay = colors.slice(-2, -1);console.log(sliceWay[0]); // 'green'// Using at() methodconst atWay = colors.at(-2);console.log(atWay); // 'green'
Calling at() on non-array objects
Theat()
method reads thelength
property ofthis
and calculates the index to access.
const arrayLike = { length: 2, 0: "a", 1: "b", 2: "c", // ignored by at() since length is 2};console.log(Array.prototype.at.call(arrayLike, 0)); // "a"console.log(Array.prototype.at.call(arrayLike, 2)); // undefined
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.at |