Avatar ·

How to get element index in for...of loop in TypeScript?

Hi everyone! I just started learning TypeScript and ran into a problem when working with loops. In JavaScript, I used to often use the regular `for (let i = 0; i < arr.length; i++)` loop to get both the element and its index. But in TypeScript, I was advised to use `for...of` because it's more readable and modern. The problem is that in `for...of` I only get the value of the element, but not its index. For example, I have an array of strings: ```typescript const fruits: string[] = ['apple', 'banana', 'orange']; ``` I want to output each element along with its number: ``` 0: apple 1: banana 2: orange ``` With a regular `for` loop this is easy to do, but with `for...of` I can't figure it out. I tried using `.entries()`, but I'm not sure if that's correct in TypeScript.