Dev Helpers

JavaScript / Collections

Iterate indexed entries

Dev Helpers · June 21, 2026

Array indexes are useful when a value occurs twice. entries() keeps the position and the value together while the loop advances.

Worked example

console.log([...["a", "b"].entries()]);

Result

[ [ 0, 'a' ], [ 1, 'b' ] ]

Implementation notes

The iterator is a view over the array as it is consumed. If another part of the loop inserts or removes entries, indexes can stop referring to the records you thought you were processing.

Try it yourself

Repeat a value at the start and end. Check that both positions survive.

Open the JavaScript example →

Editorial date June 21, 2026 · Published December 6, 2026

Related articles

Sum distances with a Euclidean norm

A diagonal is longer than either of its components. Math.hypot combines perpendicular distances without making the caller square each input.

Build an OS-independent path

Path conventions belong to the destination. Use an explicit path flavour when generating portable asset names.

Resolve ranked ties

A leaderboard needs an explicit tie policy. Ranking tied scores and sorting their display order are separate decisions.