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
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 »
Browser Support
typed-array.at()
is an ES2022 feature.
JavaScript 2022 is supported in all modern browsers since March 2023:
Chrome 94 | Edge 94 | Firefox 93 | Safari 16.4 | Opera 79 |
Sep 2021 | Sep 2021 | Oct 2021 | Mar 2023 | Oct 2021 |