Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JavaScript Array at()

ES2022 introduced the at() method for arrays.

Examples

Access the third element of the fruits array.

const fruits = [“Banana”“Orange”“Apple”“Mango”];
let fruit = fruits.at(2);

Retrieve the third element from the fruits array.

const fruits = [“Banana”“Orange”“Apple”“Mango”];
let fruit = fruits[2];

The at() method returns an element at a specified index in an array.

It behaves the same as using traditional array indexing ([]).

The at() method has been supported in all modern browsers since March 2022.

js 13

Note: Many programming languages support negative indexing, such as [-1], to access elements from the end of an array, object, or string.

In JavaScript, this isn’t possible because [] is used for both array and object access. For example, obj[-1] would reference the property with the key -1, not the last property of the object.

To address this, the at() method was introduced in ES2022.