JavaScript Array at()

The at() method returns an indexed element from an array.

Examples

Get the third element of fruits:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let index = 2;
let fruit = fruits.at(index);
Try it Yourself »

Get the third element of fruits:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let index = 2;
let fruit = fruits[2];
Try it Yourself »

More examples below.


Description

The at() method returns the same as [].


Syntax

array.at(index)

Parameters

Parameter Description
index Optional.
The index (position) of the array element to be returned.
Default is 0.
-1 returns the last element.

Return Value

Type Description
ElementThe element of the given position (index) in the array.


Browser Support

JavaScript Array at() is supported in all browsers since March 2022:

Chrome 66 Edge 79 Firefox 61 Safari 12 Opera 50
Jul 2021 Jul 2021 Jul 2021 Mar 2022 Aug 2021

More Examples

Get the first element of fruits:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits.at();
Try it Yourself »

Get the last element of fruits:

const fruits = ["Banana", "Orange", "Apple", "Mango"];
let fruit = fruits.at(-1);
Try it Yourself »

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.