typedArray.keys() returns a new iterator that gives you the indexes of the typed array: 0, 1, 2, and so on up to length - 1. It does not give the element values. You read the indexes with a for...of loop, step by step with next(), or all at once with Array.from(). A typed array never has holes, so every index is produced.
Remember
typedArray.keys() → iterator of indexes
new Uint8Array([9, 8, 7]).keys() → 0, 1, 2
new Float32Array(0).keys() → nothing (empty)
Array.from(ta.keys()) → [0, 1, 2]
for (const i of ta.keys()) { ... }
Syntax
keys() takes no arguments. Call it on any typed array.
JavaScript
typedArray.keys()
Call
What it does
ta.keys()
Get an iterator over the indexes 0 to length - 1.
Array.from(ta.keys())
Collect every index into a regular array.
ta.keys().next()
Read the first step: { value: 0, done: false }.
Built into JavaScript since ES2015 on all 11 typed array types, from Int8Array to BigUint64Array. The indexes are always plain numbers, even on a BigInt64Array.
Library declarations
JavaScript
TypedArray.prototype.keys() // returns an Array Iterator
// iterator.next() -> { value: index, done: false }
// after the last index -> { value: undefined, done: true }
// the iterator is itself iterable: for...of, spread, Array.from
Parameters
Parameter
Description
(none)
keys() has no parameters. Extra arguments are ignored.
Return Value
Return Value
Description
Array Iterator
A new iterator object. Each next() call returns the next index as { value, done }. It is the same kind of iterator that Array keys() returns.
JavaScript Examples
Example 1 — Loop Over the Indexes With for...of
A week of temperatures is stored in an Int16Array because some days are below zero. The index tells us which day we are on.
JavaScript
const temps = new Int16Array([18, 21, -3, 25]);
for (const i of temps.keys()) {
console.log('day ' + (i + 1) + ': ' + temps[i] + ' C');
}
The loop variable is a position. Each turn of for...of over temps.keys() hands us the next index: 0, then 1, 2, and 3. It is never the temperature itself.
Index in, value out. With the index in hand, temps[i] reads the stored number, including the negative -3 that an Int16Array can hold.
Friendly day numbers. Indexes start at 0, so the message adds 1 to print day 1 to day 4 for the four elements.
Example 2 — Collect the Indexes With Array.from()
Sound levels from a microphone fit in a Uint8Array. We turn the indexes into a normal array so we can filter them.
JavaScript
const levels = new Uint8Array([40, 210, 90, 255, 12]);
const indexes = Array.from(levels.keys());
console.log(indexes.join(', ')); // 0, 1, 2, 3, 4
const loud = indexes.filter((i) => levels[i] > 200);
console.log('loud at index ' + loud.join(' and ')); // loud at index 1 and 3
console.log(levels.keys().next().value); // 0
Two elements, two steps. The first two next() calls return the indexes 0 and 1 with done set to false.
The end is signalled by done. The third call has no index left, so value is undefined and done is true. A for...of loop stops right here.
Used up and empty iterators. The finished iterator gives nothing more, so Array.from(it) has length 0. A typed array with no elements also yields no keys at all.
Compatibility
Browser & Runtime Support
TypedArray.prototype.keys() is part of ES2015 (ES6), together with typed arrays, iterators, and for...of.
✓ Baseline · ES2015
TypedArray.prototype.keys()
Supported in every modern browser, plus Node.js, Deno, and Bun.
98%Modern browser support
Google ChromeAll modern versions
Full support
Mozilla FirefoxAll modern versions
Full support
Apple SafariAll modern versions
Full support
Microsoft EdgeAll modern versions
Full support
Node.jsAll current releases
Supported
DenoAll releases
Supported
Internet ExplorerIE 10 and 11 have typed arrays, but not this method
Not supported
keys()Excellent
Bottom line: Safe everywhere modern JavaScript runs. Only Internet Explorer is missing it: IE 10 and 11 have typed arrays, but no iterators and no for...of.
Frequently Asked Questions
An iterator that produces the indexes 0, 1, 2, and so on up to length - 1. It is not an array; read it with a for...of loop or turn it into one with Array.from(ta.keys()).
keys() gives the indexes, values() gives the elements, and entries() gives [index, value] pairs. All three return iterators and all three have been part of typed arrays since ES2015.
It prints the iterator object, not its contents. Node.js shows something like Object [Array Iterator] {} and browsers show their own format. Convert it to text first, for example Array.from(ta.keys()).join(", ").
No. After the last index it keeps answering done: true. Call keys() again whenever you need a new loop over the indexes.
No. Object.keys(ta) returns an array of strings such as ["0", "1", "2"]. ta.keys() returns an iterator of real numbers 0, 1, 2, which is what you want for math with indexes.