typedArray.entries() returns a new iterator of [index, value] pairs. Each step gives a small normal array with two items: the index first, then the element stored at that index. It is the easiest way to get both the position and the value in a for...of loop, usually with destructuring: for (const [i, v] of ta.entries()).
Remember
typedArray.entries() → iterator of [index, value]
new Int8Array([5, -6]).entries() → [0, 5], [1, -6]
for (const [i, v] of ta.entries()) { ... }
Array.from(ta.entries()) → [[0, 5], [1, -6]]
new Map(ta.entries()) → Map { 0 => 5, 1 => -6 }
Syntax
entries() takes no arguments. Call it on any typed array.
JavaScript
typedArray.entries()
Call
What it does
ta.entries()
Get an iterator of [index, value] pairs.
for (const [i, v] of ta.entries())
Loop with the index and the value in two named variables.
Array.from(ta.entries())
Collect all pairs into an array of two-item arrays.
Built into JavaScript since ES2015 on all 11 typed array types. The index is always a number; the value has the element type (a BigInt on BigInt64Array and BigUint64Array).
Library declarations
JavaScript
TypedArray.prototype.entries() // returns an Array Iterator
// iterator.next() -> { value: [index, element], done: false }
// after the last element -> { value: undefined, done: true }
// each pair is a new, normal two-item Array
Parameters
Parameter
Description
(none)
entries() has no parameters. Extra arguments are ignored.
Return Value
Return Value
Description
Array Iterator
A new iterator object. Each next() call returns { value: [index, element], done }. The pair is a regular Array, not a typed array.
JavaScript Examples
Example 1 — Destructure [index, value] in a Loop
Daily step counts sit in a Uint16Array. Destructuring each pair gives two clear variable names.
JavaScript
const steps = new Uint16Array([5200, 8100, 3900]);
for (const [day, count] of steps.entries()) {
console.log('Day ' + day + ' -> ' + count + ' steps');
}
Day 0 -> 5200 steps
Day 1 -> 8100 steps
Day 2 -> 3900 steps
How It Works
Each turn yields a pair.steps.entries() produces [0, 5200], [1, 8100], and [2, 3900], one pair per loop turn.
Destructuring names both parts. The pattern [day, count] puts the first item of the pair in day and the second in count, so there is no pair[0] noise.
Indexes still start at zero. The printed day numbers are the raw indexes 0, 1, and 2, showing that entries() counts positions from 0 like every typed array.
Example 2 — Remember Where the Maximum Was
To report both the largest reading and its position, we keep the best pair seen so far. The first maximum wins a tie.
JavaScript
const readings = new Float64Array([3.5, 9.25, 9.25, 1]);
let best = [-1, -Infinity];
for (const entry of readings.entries()) {
if (entry[1] > best[1]) best = entry;
}
console.log('max ' + best[1] + ' at index ' + best[0]); // max 9.25 at index 1
console.log(Array.isArray(best)); // true
console.log(best.length); // 2
Collect the pairs first.Array.from(codes.entries()) turns the iterator into an array of three pairs, and JSON.stringify prints it in full.
An iterator alone prints as {}. Passing the iterator straight to JSON.stringify shows {}, because the iterator object has no data properties of its own.
Pairs feed a Map directly.new Map(codes.entries()) uses each index as a key and each element as its value, so get(2) returns 7 and the Map holds 3 entries.
Compatibility
Browser & Runtime Support
TypedArray.prototype.entries() is part of ES2015 (ES6), together with typed arrays, iterators, and destructuring.
✓ Baseline · ES2015
TypedArray.prototype.entries()
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
entries()Excellent
Bottom line: Safe everywhere modern JavaScript runs. In Internet Explorer 10 and 11, which have typed arrays but no iterators, use a plain for loop with an index instead.
Frequently Asked Questions
An iterator of [index, value] pairs. Each pair is a normal two-item Array: the index comes first, then the element at that index.
No. Each pair is a regular Array. The index is a number and the value has the element type, for example a BigInt on a BigInt64Array, so changing the pair does not change the typed array.
Use for (const [i, v] of ta.entries()). Another option is ta.forEach((value, index) => ...), which passes the value first and the index second.
The iterator object has no data properties of its own, so JSON sees an empty object. Convert it first: JSON.stringify(Array.from(ta.entries())).
Yes. new Map(ta.entries()) creates a Map whose keys are the indexes and whose values are the elements, because each pair already has the [key, value] shape a Map needs.