Typed Array at()
Examples
const myArr = Int32Array.of(1,2,3,4,5,6);
let myNumber = myArr.at(0);
const myArr = Int32Array.of(1,2,3,4,5,6);
let myNumber = myArr.at(0);
More examples below
Description
Theat() method returns an indexed element from a typed array.
Theat() method returns the same as[].
Note
Many languages allowsnegative bracket indexing like [-1] to access elements from the end of anobject / 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.
Theat() method for arrays was introduced in ES2022 to solve this problem.
Syntax
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:
const myArr = Int32Array.of(1,2,3,4,5,6)
let myNumber = myArr.at();
Return the last element:
const myArr = Int32Array.of(1,2,3,4,5,6)
let myNumber = myArr.at(-1);
Browser Support
typed-array.at() is a JavaScript 2022 feature.
ES 2022 is supported in all modern browsers sinceMarch 2023:
| Chrome 94 | Edge 94 | Firefox 93 | Safari 16.4 | Opera 79 |
| Sep 2021 | Sep 2021 | Oct 2021 | Mar 2023 | Oct 2021 |

