Array.prototype.at()

Varun Singhal
Sep 29, 2022

--

The at() method 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.

The index (position) of the array element to be returned. Supports relative indexing from the end of the array when passed a negative index; i.e. if a negative number is used, the element returned will be found by counting back from the end of the array.

const array1 = [5, 12, 8, 130, 44];

let index = 2;

console.log(`Using an index of ${index} the item returned is ${array1.at(index)}`);
// expected output: “Using an index of 2 the item returned is 8”

index = -2;

console.log(`Using an index of ${index} item returned is ${array1.at(index)}`);
// expected output: “Using an index of -2 item returned is 130”

--

--

Varun Singhal
Varun Singhal

Written by Varun Singhal

React Native/iOS Developer (Mobile App Developer)

No responses yet