I’ve worked on a bunch of data-heavy UI interfaces, and trust me, clean iteration matters.
Here’s the most elegant method I always come back to—using .entries()
with for of
. It gives you both index and value without breaking the flow of the loop:
const nums = [10, 20, 30];
for (const [index, value] of nums.entries()) {
console.log(`Index: ${index}, Value: ${value}`);
}
Why this works: If you’re looking for a readable way to get the for of JavaScript index, this feels natural and keeps things tidy—especially when you’re mapping indexes to UI elements.