.at()

Sumit_27's avatar
Published Jul 30, 2023
Contribute to Docs

The .at() method in JavaScript is used to return the element at a specified index in an array. The index can be either positive or negative (negative indices count from the end of the array).

Syntax

array.at(index);
  • index: The index value of the element to be returned.

Example

The example below demonstrates how to use the .at() function to extract an element from an array based on an index.

const array = [1, 2, 3, 4, 5];
const firstElement = array.at(0);
console.log(firstElement);

This example results in the following output:

1

All contributors

Contribute to Docs

Learn JavaScript on Codecademy