at() – Get Item from Array at Given Index
array1.at(index)
array1
is an array of itemsindex
is the position of item that is of interest
at() method returns the item at given position index
The index starts at zero for the items in array. 0 is the index for first item, 1 is the index for the second item, and so on.
Examples
1. Get item from array array1 at index=3.
let array1 = [2, 4, 8, 16, 32, 64, 128, 256];
let index = 3;
let item = array1.at(index);
console.log('Item : ' + item);
2. No index passed to at() method.
let array1 = [2, 4, 8, 16, 32, 64, 128, 256];
let item = array1.at();
console.log('Item : ' + item);
If no index is passed to at() method, it returns the first item, just like index=0.
3. Negative index to at() method.
let array1 = [2, 4, 8, 16, 32, 64, 128, 256];
let item = array1.at(-3);
console.log('Item : ' + item);
If negative index is passed to at() method, then it returns the item at the index from the end of the array. -1 is the index of last item, -2 is the index of last but one, and so on.