JavaScript at()Method

Array 对象参考手册JavaScript Array Object

Example

Takes an integer value and returns the element at that index:

const array1 = [5, 12, 8, 130, 44]; let index1 = 2; strt1= `The index is ${index1}and the value is ${array1.at(index1)}`; document.getElementById("demo1").innerHTML = strt1; let index2 = -2; strt2= `The index is ${index2}and the value is ${array1.at(index2)}`; document.getElementById("demo2").innerHTML = strt2;

Try it Yourself »

Definition and Usage

The at() method takes an integer value and returns the element at that index, allowing positive and negative integers. Negative integers count back from the last element in the array.

Returns the element in the array matching the given index. If the specified index is not found, it returns undefined.

When passing a non-negative number, the at() method is equivalent to bracket notation. For example, array

The usual practice is to access length and subtract the relative index from the end. For example, array[array.length - 1]. The at() method allows relative indexing, so the above example can be simplified to array.at(-1). More formally, when index < 0, the method accesses the index index + array.length.

The at() method is generic. It only expects this to have a length property and properties with integer keys.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method
at() 45.0 12.0 32.0 9 25.0

Syntax

at(index)

Parameters

Parameter Description
index The index (position) of the array element to return. When a negative number is passed, relative indexing from the end of the array is supported; that is, if a negative number is used, the returned element will be counted backwards from the end of the array.

Technical Details

Return value: Element.
JavaScript Version: ECMAScript 6

More Examples

Return the last value of an array:The following example provides a function that returns the last element found in a specified array.

Example

// Array and array elements
const cart = ['apple', 'banana', 'pear'];

// A function to return the last element of a given array
function returnLast(arr) {
  return arr.at(-1);
}

// Get the last element of the 'cart' array
const item1 = returnLast(cart);
console.log(item1); // Output: 'pear'

// Add an element to the 'cart' array
cart.push('orange');
const item2 = returnLast(cart);
console.log(item2); // Output: 'orange'

Compare different array methods:The following example compares different ways to select the second-to-last element of an Array. While all the methods shown below are valid, this example highlights the conciseness and readability of the at() method.

Example

// Array and array elements
const colors = ['red', 'green', 'blue'];

// Using the length property
const lengthWay = colors[colors.length-2];
console.log(lengthWay); // Output: 'green'

// Using the slice() method. Note that it returns an array
const sliceWay = colors.slice(-2, -1);
console.log(sliceWay[0]); // Output: 'green'

// Using the at() method
const atWay = colors.at(-2);
console.log(atWay); // Output: 'green'

Calling at() on non-array objects: The at() method reads the length property of this and calculates the index to access.

Example

const arrayLike = {
  length: 2,
  0: "a",
  1: "b",
};
console.log(Array.prototype.at.call(arrayLike, -1)); // "b"


Array 对象参考手册JavaScript Array Object

Other Extensions