Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

at

Array.prototype.at() returns the element at a given integer index and supports negative indexes that count from the end. It returns undefined when the index is outside the array's bounds.

How it works

Call at(index) on an array to read one element. The index is converted to an integer; non-negative values count from the beginning, while negative values are added to the array length so they count backward from the end. An index outside the array produces undefined.

Syntax

at(index)

Parameters

NameTypeDescription
indexintegerA zero-based index, converted to an integer. Negative values count from the end of the array.

Returns

The element at the selected index, or undefined when the index is outside the array's bounds.

Key points

  • at() reads and returns the element selected by an integer index.
  • A negative index selects relative to the array's end; -1 means the final element.
  • Indexes are zero-based and the supplied value is converted to an integer.
  • For non-negative integer indexes, at() and bracket notation select the same element.
  • The method can operate on array-like objects that provide the required indexed structure.
  • Lookup uses the receiver's length to calculate the actual property index.

Common mistakes

  • Bracket notation does not support relative negative indexing: array[-1] reads the property named "-1", not the last element.
  • Out-of-range indexes return undefined, and the corresponding property is not accessed.

Example: Read array elements with relative indexes

const colors = ["red", "green", "blue"];

console.log(colors.at(0));  // "red"
console.log(colors.at(-1)); // "blue"
console.log(colors.at(99)); // undefined

Use a non-negative index for ordinary access and a negative index to count from the end.

When to use it

Access from the end

Read the most recent item without manually subtracting from length.

Index-based access

Use a concise, readable alternative to array[index] for ordinary element lookup.

Work with array-like data

Read values from objects that expose length and integer-keyed properties.

Interview questions

QWhat does Array.prototype.at() do?

at(index) returns the element at a zero-based index. Negative indexes count backward from the array's end, so at(-1) returns the final element; an out-of-range index returns undefined.

Follow-up: How does at() differ from bracket notation for negative indexes? Bracket notation treats -1 as the string property "-1", whereas at(-1) performs relative indexing.

QHow are positive and negative indexes interpreted?

For a non-negative integer, at(index) is equivalent to array[index]. Its distinctive feature is that a negative index is translated relative to the array length.

Follow-up: What happens for a fractional index? The argument is converted to an integer before the element lookup.

QIs at() limited to Array instances?

The method is generic: it needs a this value with a length property and integer-keyed properties. Therefore, it can read from suitable array-like objects, not only actual arrays.

Follow-up: Can it be used on a typed array? TypedArray.prototype.at() has the same algorithm, but the typed-array method itself is not generic.

  • Array.prototype.with()
  • Array.prototype.slice()
  • TypedArray.prototype.at()

Source: Array.prototype.at() - JavaScript | MDN. Checked 2026-10-15.