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!
For Loop to Traverse Arrays. We can use iteration with a for loop to visit each element of an array. This is called traversing the array. Just start the index at 0 and loop while the index is less than the length of the array.
You can declare (define) an array inside a loop, but you won't be able to use it anywhere outside the loop. You could also declare the array outside the loop and create it (eg. by calling new ...) inside the loop, in which case you would be able to use it anywhere as far as the scope the declaration is in goes.
Actually, it has very much to do with the question (specifically, yes, you CAN use a string as an index, but not in the obvious way that the original querier wants).
You want to explicitly add it at a particular place of the array. That place is called the index. Array indexes start from 0 , so if you want to add the item first, you'll use index 0 , in the second place the index is 1 , and so on. To perform this operation you will use the splice() method of an array.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With