Languages

JavaScript - why shouldn't I use for…in statement to iterate arrays?

3 points
Asked by:
Walter
586

Recently someone told me not to use for...in statement to iterate arrays. Can you tell me why?

2 answers
4 points
Answered by:
Daniel Setzer
1040

Directly from the manual:

The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties. While Array indexes are just enumerable properties with integer names, otherwise identical to general object properties, there is no guarantee that for...in will return the indexes in any particular order.

Because the order of iteration is implementation-dependent, iterating over an array may not visit elements in a consistent order. Therefore, it is better to use a for loop with a numeric index (or Array.prototype.forEach() or the for...of loop) when iterating over arrays where the order of access is important.

The other answer is way too is uncessarily thorough. If you want to be a developer you need to get used to reading the documentation for things. At least the documentation for the actual language itself..

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

From my personal point of view, yeah it does seem logical to assume because of the "in" that you're iterating the items "in" the array, but this is designed for iterating the properties "in" an object, it was never made or meant for arrays even though they can be used, it's only because Arrays are also objects.

If you want to iterate over the values of an object including the values of an Array there's an alternate type of statement that of couse is the for...of statement

 



 

0 commentsAdd comment
0 points
Answered by:
Admin Dirask Community
4380

The for...in statement is not used to iterate arrays because it iterates over keys and properties.

It is also considered bad practice for iterating through arrays because of some use cases.

Example 1

Let's take a look at the code below:

In this example for loop prints each item including undefined ones.

However, when we use for...in statement, it will ignore the undefined keys and display only the value under the index 3.

Example 2

JavaScript also has problems when you add something to the array prototype e.g:

As you see the result is not as we expected.

Example 3

An array is an object that can be extended with additional fields

0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join