Typed Array at()
Examples
// Create a Typed Array
const myArr = Int32Array.of(1,2,3,4,5,6)
let myNumber = myArr.at(0);
Try it Yourself »
// Create a Typed Array
const myArr = Int32Array.of(1,2,3,4,5,6)
let myNumber = myArr.at(0);
Try it Yourself »
More examples below
Description
The at()
method returns an indexed element from a typed array.
The at()
method returns the same as []
.
Note
Many languages allows negative bracket indexing
like [-1] to access elements from the end of an
object / array / string.
This is not possible in JavaScript, because [] is used for accessing both arrays and objects.
Because of this, obj[-1] refers to the value of key -1, not to the last property of the object.
The at()
method for arrays was introduced in ES2022 to solve this problem.
Syntax
typed-array.at(index)
typed-array must be one of the following: Int8ArrayUint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float16Array Float32Array Float64Array BigInt64Array BigUint64Array |
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 |
Element | The element in the given position (index) in the array. |
JavaScript Typed Arrays
Browser Support
JavaScript Array at()
is supported in all browsers since March 2022:
Chrome 92 | Edge 92 | Firefox 90 | Safari 15.4 | Opera 78 |
Apr 2021 | Jul 2021 | Jul 2021 | Mar 2022 | Aug 2021 |
More Examples
Return the first element:
// Create a Typed Array
const myArr = Int32Array.of(1,2,3,4,5,6)
let myNumber = myArr.at();
Try it Yourself »
Return the last element:
// Create a Typed Array
const myArr = Int32Array.of(1,2,3,4,5,6)
let myNumber = myArr.at(-1);
Try it Yourself »