Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I've been told not to use for...in with arrays in JavaScript. Why not?

1 Answer

0 votes
by (106k points)

Why we say the for-in statement is a "bad practice" is because it can be misused, for example, to iterate over arrays or array-like objects.

So, the main purpose of the for-in statement is to enumerate over object properties. This statement will go up in the prototype chain, also enumerating over inherited properties, a thing that sometimes is not desired.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...