Why using for...in for iterating over an array is a bad idea?
Hi! I recently started learning JavaScript and noticed that `for...in` is often used to iterate over object properties. I thought that since an array is also an object, I could use the same loop for it. I wrote this code:
```javascript
const numbers = [10, 20, 30, 40];
for (let index in numbers) {
console.log(numbers[index]);
}
```
It seems to work — it outputs 10, 20, 30, 40. But I noticed that many experienced developers criticize this approach and say it's a "bad practice". Why? The code works! What problems can arise from using `for...in` for arrays? And what should I use instead? Please explain with examples so I can understand once and for all. Thanks in advance!
<<>>
Why for...in is not intended for arrays
The loop
No answers yet.
Log in to leave an answer