I have been reading Object-Oriented Javascript by Stoyan Stefanov, and at one point he writes:
The
for-in
loop is used to iterate over the element of an array (or an object, as we'll see later). This is it's only use; it can not be used as a general-purpose repetition mechanism that replacesfor
orwhile
. Let's see an example of using afor-in
to loop through the elements of an array. But bear in mind that this is for informational purposes only, asfor-in
is mostly suitable for objects, and the regularfor
loop should be used for arrrays.
I have always used for
loops in the past when iterating of the elements of an array and I have usually seen for
loops not for-in
loops used for this purpose, but what is the reason that the "regular for
loop should be used for arrays"?