25

When I write some javascript such as this:

var words = ['word1', 'word2', 'word3']
for (word in words) {
 console.log(word)
}

The resulting output is the numeric indices of the respective word. I did some searching on Google and I couldn't find the exact reason for this behavior. I'm guessing that this is completely expected behavior but I would like to know the reason why.

Thanks!

asked Sep 20, 2011 at 4:05
2

4 Answers 4

44

Why is nobody providing the correct answer? You NEVER iterate arrays with a for/in loop - that is only for iterating plain objects and their keys, not for iterating the items in an array.

You iterate arrays for a for loop like this:

var words = ['word1', 'word2', 'word3'];
for (var i = 0, len = words.length; i < len; i++) {
 // here words[i] is the array element
}

Or you can use the .forEach() method of arrays:

var words = ['word1', 'word2', 'word3'];
words.forEach(function(value, index, array) {
 // here value is the array element being iterated
});

See here at MDN for more info on .forEach().

ndp's reference to this post shows some good examples of things that can go wrong using for/in with arrays. You can make it works sometimes, but it is not the smart way to write Javascript array iteration.


Or, in more modern times, you can use the ES6 for/of construct which is specially built for iterating an iterable (an array is an iterable):

var words = ['word1', 'word2', 'word3'];
for (const w of words) {
 console.log(w);
}

Or, if you want both the value and the index, you can do this:

var words = ['word1', 'word2', 'word3'];
for (const [index, w] of words.entries()) {
 console.log(index, ": ", w);
}

There are several advantages of for/of over .forEach(). To start with, you have more loop control as you can use break, continue, return to control the loop flow which .forEach() does not give you. In addition, there's no additional overhead for a callback function so, in some environments, it can be faster.

answered Sep 20, 2011 at 4:32
Sign up to request clarification or add additional context in comments.

1 Comment

Added ES6 options.
2

As the other answers correctly point out, for...in is not for iterating over arrays. A better option is now available in newer JavaScript engines and transpilers like TypeScript with for...of:

var words = ['word1', 'word2', 'word3']
for (word of words) {
 console.log(word)
}
answered Aug 15, 2016 at 15:51

Comments

1
  1. for..in is used to iterate over the properties of a javascript object.
  2. Think of an array as a javascript object with indexes as properties.
answered Sep 20, 2011 at 4:15

Comments

1

For ... in is intended for objects -- see this question. Apparently it can (and is) used for arrays, but this has a few risks you may not want.

answered Sep 20, 2011 at 4:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.