JavaScript at() method
Example
Accept an integer value and return the element at that index:
const
array1
=
[
5
,
12
,
8
,
130
,
44
]
;
let
index1
=
2
;
strt1
= `Index number is $
{
index1
}
, value is $
{
array1
.
at
(
index1
)
}
`;
document
.
getElementById
(
"
demo1
"
)
.
innerHTML
=
strt1
;
let
index2
= -
2
;
strt2
= `Index number is $
{
index2
}
, value is $
{
array1
.
at
(
index2
)
}
`;
document
.
getElementById
(
"
demo2
"
)
.
innerHTML
=
strt2
;
Try it »
Definition and Usage
The at() method accepts an integer value and returns the element at that index, allowing positive and negative numbers. Negative integers count backward from the last element of the array.
The element in the array that matches the given index. If the specified index is not found, undefined is returned.
When passing a non-negative number, the at() method is equivalent to bracket notation. For example, array[0] and array.at(0) both return the first element. However, when you need to count backward from the end of the array, you cannot use array[-1] as supported in Python and R, because all values inside the brackets are treated as string properties, so you end up reading array["-1"], which is just an ordinary string property rather than an array index.
More formally, when the index < is >= 0, the method accesses the index index + array.length.
The at() method is generic. It only expects this to have a length property and properties keyed by integers.
Browser support
The numbers in the table indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| at() | 45.0 | 12.0 | 32.0 | 9 | 25.0 |
Syntax
at(index)
Parameters
| Parameters | Description |
|---|---|
| index | The index (position) of the array element to return. When a negative number is passed, relative indexes from the end of the array are supported; that is, if a negative number is used, the returned element is counted backward from the end of the array. |
Technical Details
| Return value: | . |
|---|---|
| JavaScript version: | ECMAScript 6 |
More examples
Return the last value of the array: The following example provides a function that returns the last element found in a specified array.
Example
// Array and array elements
const
cart
=
[
'apple'
,
'banana'
,
'pear'
]
;
// A function used to return the last element of a given array
function
returnLast
(
arr
)
{
return
arr.
at
(
1
)
;
}
// Get the last element of the 'cart' array
const
item1
=
returnLast
(
cart
)
;
console.
log
(
item1
)
;
// Output: 'pear'
// Add an element to the 'cart' array
cart.
push
(
'orange'
)
;
const
item2
=
returnLast
(
cart
)
;
console.
log
(
item2
)
;
// Output: 'orange'
Compare different array methods: The following example compares different methods for selecting the second-to-last element in an Array. Although all the methods shown below are workable, this example highlights the conciseness and readability of the at() method.
Example
// Array and array elements
const
colors
=
[
'red'
,
'green'
,
'blue'
]
;
// Use the length property
const
lengthWay
=
colors
[
colors.
length
2
]
;
console.
log
(
lengthWay
)
;
// Output: 'green'
// Use the slice() method. Note that it returns an array
const
sliceWay
=
colors.
slice
(
2
,
1
)
;
console.
log
(
sliceWay
[
0
]
)
;
// Output: 'green'
// Use the at() method
const
atWay
=
colors.
at
(
2
)
;
console.
log
(
atWay
)
;
// Output: 'green'
Calling at() on non-array objects: The at() method reads this's length property and calculates the index to access.
Example
const
arrayLike
=
{
length
:
2
,
0
:
"a"
,
1
:
"b"
,
}
;
console.
log
(
Array
.
prototype
.
at
.
call
(
arrayLike
,
1
)
)
;
// "b"
JavaScript Array object