at method
The at
method returns the
array element at the given position. The
method parameter specifies a positive
or negative integer. In the first case,
the element is searched from the
beginning of the array, in the second,
from the end.
Syntax
array.at(index);
Example
Let's find the first element of an array:
let res = ['a', 'b', 'c'].at(0);
console.log(res);
The code execution result:
'a'
Example
Now let's set the method parameter to a negative number:
let res = ['a', 'b', 'c'].at(-1);
console.log(res);
The code execution result:
'c'
Example
And now let's find the element by specifying a fractional number in the parameter:
let res = ['a', 'b', 'c'].at(1.8);
console.log(res);
As you can see from the result, the method returns an element that corresponds to the integer part of a fractional number:
'b'
See also
-
the
Array.of
method
that obtains an array from parameters