typedArray.at(index) returns the element at the given position. It works like ta[index], with one big bonus: a negative index counts from the end, so ta.at(-1) is the last element. If the index is outside the typed array, the result is undefined. The method arrived in ES2022, together with at() on regular arrays and strings.
Remember
typedArray.at(index) → the element, or undefined
new Uint8Array([10, 20, 30]).at(0) → 10
new Uint8Array([10, 20, 30]).at(-1) → 30 (last)
new Uint8Array([10, 20, 30]).at(3) → undefined
ta[-1] → undefined (NOT the last element!)
Syntax
at() takes one argument: the position. Call it on any typed array.
JavaScript
typedArray.at(index)
Call
What it does
ta.at(0)
Read the first element.
ta.at(-1)
Read the last element. -2 is the one before it.
ta.at(ta.length)
One past the end, so the result is undefined.
ES2022 method, available on all 11 typed array types from Int8Array to BigUint64Array. The result has the element type: a number, or a BigInt for the two BigInt arrays.
Library declarations
JavaScript
TypedArray.prototype.at(index) // returns the element or undefined
// index >= 0 -> counts from the start
// index < 0 -> counts from the end: index + length
// out of range -> undefined
Parameters
Parameter
Description
index
The position to read. It is converted to a whole number (1.9 becomes 1, NaN and a missing argument become 0). A negative value counts back from the end.
Return Value
Return Value
Description
Number, BigInt, or undefined
The element at that position, or undefined when the index is too large or too small. Compare with Array at(), which behaves the same way.
JavaScript Examples
Example 1 — Read From the Start and From the End
Four temperatures are stored in an Int16Array. A positive index counts from the start and a negative index counts from the end.
Only at() reads from the end.bytes.at(-1) gives 30, the last byte, while bytes[-1] looks for a property named "-1" and finds nothing.
Too big is undefined. There are three bytes, so valid positions are 0 to 2. Position 3 is outside and at(3) answers undefined instead of throwing an error.
Too negative is undefined.-4 would mean index -1 after counting from the end, which is also out of range, so no value exists there either.
Example 3 — The Index Is Turned Into a Whole Number
The argument does not have to be a clean integer. A Float32Array also shows that the element comes back in the stored single-precision form.
Stored form, not typed form. The first element was written as 0.1, but a Float32Array keeps single precision, so at(0) returns 0.10000000149011612.
Decimals are cut off.1.9 loses its fraction and becomes index 1, which holds 2.5. A string such as "2" is converted to the number 2 and reads 7.
Nothing means zero. With no argument the index becomes 0, so the first element comes back. -1.9 is cut to -1 and reads the last element.
Compatibility
Browser & Runtime Support
TypedArray.prototype.at() is part of ES2022, so it needs a recent browser or runtime.
✓ Baseline · ES2022
TypedArray.prototype.at()
Chrome 92, Firefox 90, Safari 15.4, Edge 92, and Node.js 16.6 added it.
95%Recent browsers only
Google ChromeChrome 92 and newer
Full support
Mozilla FirefoxFirefox 90 and newer
Full support
Apple SafariSafari 15.4 and newer
Full support
Microsoft EdgeEdge 92 and newer
Full support
Node.jsNode.js 16.6 and newer
Supported
DenoRecent versions
Supported
Internet ExplorerNo support
Not supported
at()Recent browsers
Bottom line: Fine for current browsers and servers. For very old environments, read the last element with ta[ta.length - 1].
Frequently Asked Questions
Use ta.at(-1). The older way is ta[ta.length - 1]. Both give the same number; at(-1) is shorter and easier to read.
Square brackets only accept valid element positions, and -1 is not one. The engine does not treat it as "count from the end". Only at() does that.
No. An index that is too large or too small just returns undefined. This is the same as bracket notation for positive indexes.
The idea is the same. The typed version works on one number type and never has holes, so the result is always a number (or BigInt), or undefined when out of range.
No. It only reads one element. To replace an element at a position without changing the original, see the with() method.