Iterating Through Arrays
When we use the word iterate, it means to go through an array, or go through every element in an array. An array is an object that can store multiple properties or values in a single variable. For an example we could use firstNames as a variable.
Here we have firstNames which is also an object. And there are 4 elements in this firstNames array. In JavaScript elements in an array starts at the 0 position. So ‘Daniel’ would be at the 0 position, ‘John’ is at the 1 position and so on. Now in order to iterate or go through the array we would have to use a JavaScript method. In JavaScript there are many methods that can be used to iterate through an array.
For this example i’m going to use a for loop. You can get more info on the for loop here on Mozilla Development Network(MDN).
So if I wanted to iterate through all the elements in my firstNames array i would type:
for (var i = 0; i < firstNames.length; i++) {
console.log(firstNames[i]);
}
And what this code snippet/method would do is go through every element in my firstNames array. There are many other methods to use that can do the same and much more. You can get read more on arrays and methods here on MDN. It’s one of the best sources to get documentation of JavaScript.
