JavaScript TypedArray keys() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ES2015

Definition

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()
CallWhat 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

ParameterDescription
(none)keys() has no parameters. Extra arguments are ignored.

Return Value

Return ValueDescription
Array IteratorA 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');
}
Try It Yourself

How It Works

  1. 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.
  2. Index in, value out. With the index in hand, temps[i] reads the stored number, including the negative -3 that an Int16Array can hold.
  3. 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
Try It Yourself

How It Works

  1. Iterator to real array. Array.from(levels.keys()) runs the iterator to the end and stores every index in a regular Array of five numbers.
  2. Filter by the stored level. The test levels[i] > 200 keeps only the positions whose level is above 200, which are index 1 (210) and index 3 (255).
  3. A fresh iterator starts at 0. Calling keys() again creates a brand-new iterator, so its first next() gives index 0 again.

Example 3 — Step Through With next() Until done

An iterator is a small object with a next() method. Calling it by hand shows exactly how a loop sees the indexes, and what happens at the end.

JavaScript
const samples = new Float32Array([0.5, 1.5]);
const it = samples.keys();

let step = it.next();
console.log(step.value + ' ' + step.done);  // 0 false
step = it.next();
console.log(step.value + ' ' + step.done);  // 1 false
step = it.next();
console.log(step.value + ' ' + step.done);  // undefined true

console.log(Array.from(it).length);                          // 0
console.log(Array.from(new Float32Array(0).keys()).length);  // 0
Try It Yourself

How It Works

  1. Two elements, two steps. The first two next() calls return the indexes 0 and 1 with done set to false.
  2. 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.
  3. 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.

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 Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Node.js All current releases
Supported
Deno All releases
Supported
Internet Explorer IE 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.

Next: lastIndexOf()

Find the index of the last element that matches the value you are looking for.

lastIndexOf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful