More Ways to Iterate in JavaScript for Newbies
Tags: javascript, looping, beginners, webdev
Published: 2021-08-25 22:52:08

In this article, we are going to talk about methods in JavaScript to iterate (perform an action multiple times)! If you haven't read my ['For loops for Newbies'](https://dev.to/lindsfonnes/javascript-for-loops-for-newbies-3lo) article yet, I highly suggest it as it's a great starting point. 

While often the first iteration option that beginners are taught, is the 'for loop', using methods to loop is a lot more common. I appreciated being taught the 'for loop' first, because it really helped me understand what was happening before I moved on to learning other forms of iteration.

Referencing our original for loop where we printed out the names of each Sailor Guardian, we can actually do this several other ways that can save even more time than writing out a traditional 'for loop'.

![ForLoop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4babuchxkaac6donpvx.png)

Let's start with a very common array iteration method, forEach. 
To use the forEach method, you will call it upon the array that you are working with, and it will run a function for each item in that array. Some great things about using forEach include shorter syntax, along with having access to both the index and array like you would if you were using a traditional 'for loop'.

![forEach](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fr87yw838p5sypsa9z5e.JPG) 


![forEachArrowFunction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbtv7on3nvia1ztbkl5w.JPG)

Using an [arrow function](https://www.youtube.com/watch?v=h33Srr5J9nY), we can put our entire function into a single line and have it give us the same result! Here you will see exactly what we are having our forEach method do; print out the individual sailor, the index of that sailor, and the array that we are calling the method on. The index and array values are optional, and if you don't specifically need access to them in your function, feel free to leave them out. 

![ForEach Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tid4tk97tq1p1b5lpqbr.JPG)


Another benefit of using the forEach method is that we can either use an anonymous function to have our forEach directly perform the desired action, or you can pass in an already defined function. Check this out:

![Callback Function For Each](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p53u8f0ylcwb55mzivrr.JPG)

![Sparkles](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkg4wxh2icoo690qpm8m.JPG)

This is basically saying "for each sailor in my array, run the transformationSparkles function".

Similar to the forEach method, is the map method. Map takes the same parameters but the difference is that it gives you a new array. This comes in handy if you don't want your function to alter the original array. Here you can see that our newSailors array is now a completely different array, and we didn't change the values in the original sailors array.

![map code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ju0dmo4gyubvs2r3nps.JPG)

![new sailors output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o23qk43uupv0qoq8q0ck.JPG)


Besides those two popular methods, other options are 'for..of' loops and 'for..in' loops.  Don't worry if you get confused on which does what, I find myself still Googling when I can't remember for sure! The 'for..of' loop iterates over the values in an iterable object such as an array, string, or NodeList. The 'for..in' loop iterates over the property keys of an object rather than the value itself, so if you were to use it on an array, it would return the index rather than the 
value held in that index position. If you're using it on an object it will return the keys in the key-value pair.

![for of for in loop](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/422hinlp5asb0x8cv6mc.JPG)

![output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bft3hbjoojyu619hw9ta.JPG)

Above shows how each would work with an array. The 'for..of' loop is returning the array values, while the 'for..in' is just returning the indexes. For this reason, the 'for..in' is more useful when working with objects. Since 'for..in' loops gives us access to each key, we can access the values in an object by referencing the key. 

![For In loop with object](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwygs08czc2k4jeqjczc.JPG)

If you were to try using a 'for..of' loop, you would get the following error "TypeError: sailorsObj is not iterable". This is because objects are not considered iterable, so a 'for..of' loop cannot access each value in an object. Because a 'for..in' loop accesses the key, you are able to access the values in that way.

Thanks for reading! Feel free to [fork a copy of the code](https://replit.com/@lindsfonnes/MoreWaysToIterate) if you want to test out what else you can do with these iteration methods.