JavaScript 'for loops' for Newbies
Tags: javascript, beginners, programming, webdev
Published: 2021-08-11 04:05:41

When I first started to learn how to code, I remember the concept of loops having me stumped for what felt like the longest time. No matter how many videos I watched and materials that I read, I couldn't get the concept to click in my mind. My biggest frustration was that it felt like no one remembered what it was like to be a beginner and not know anything about code, all the explanations used technical terms that made it feel more confusing than it really was. I vowed that one day I would write my own explanation on loops, that explains loops the way that I wish they were explained to me!

The purpose of a for loop is when you want to repeat an action multiple times.

Without using a loop, if you wanted to console.log a phrase 10 times you could do it like so:


![Code snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5biztow7ga2zugiobtj6.PNG)


Whew! That definitely takes up a lot of space, and could be made a lot easier. What if we had to do that 100, or 1000 times?? Or what if we were wanting to console.log each value in an array (a list of values) with a lot of values in it?

In comes the power of the loop!
A for loop consists of 3 parts: the initial expression, the condition expression, and the increment expression.

All for loops will start the same way.

![code snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2cvq2szmuiakwb94gke.PNG)

Don't panic! These are the technical terms that I mentioned had me feeling flustered when I was trying to learn.
The initial expression is the count that you want your loop to start at. Usually this is 0, but you can start it at any number you want. You will declare the value that you are starting at as a variable, in this case you will use 'let' instead of 'const' because the value will be changing each time the loop runs. 

A common variable name used for the initial expression is 'i'. This typically stands for index (as in the index value of an array) but you can name it whatever you want as long as you keep using that same variable name that you assigned the initial expression to. Again, 'i' is just the name of the variable, it doesn't do anything special other than name the variable so that we can keep using it.

![code snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttcslgui3a9nubhrorx2.PNG)

One way that I liked to do was refer to 'i' as the loop itself. So if we assign let i = 0, in my mind I knew that meant the loop itself is starting at 0.

Next we have the condition expression. All this means is; how many times do you want the loop to run? If we want our loop to run 10 times (aka our action to happen 10 times), we just say it like this:

![code snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whiwye1cnpnmldyx7aa4.PNG)

By saying that we want the loop to run while 'i' is less than 10, this will have our loop run until it reaches 10 since the value starts at 0 and increases each time the loop performs it's action. This is where you need to be careful to not create an infinite loop on accident!

If we were to write it like this:

![code snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uakw1il3o8z0svouig7m.PNG)

We would have big problems here! Our loop starts at the count of 0, and then we are telling the loop that we want it to run while the loop's count is greater than 10. Once our loop runs 10 times, it will keep running because it will always be greater than 10 after that point. This can cause your browser to crash from running the loop for so long, but everyone has to create an accidental infinite loop at least once to remember why to double check how many times we are really looping.

Next we have our increment expression. Remember how I said our 'i' variable will increase each time that the loop runs? The increment expression is what causes 'i' to increase. A lot of the time, we will increase it's value by 1 each time that the loop runs, but you can increase it's value by whatever you want. Or even decrease it if you are starting 'i' at a value larger than 0.
So now all put together we have:

![code snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frt09lqyss4mvwegj0s6.PNG)

If you aren't familiar with the ++ operator, it adds 1 to our value and then returns the previous value of 'i' + 1.

Now that we have our loop constructed, we just have to do something with it! Remember the curly braces, and insert inside whatever you want to happen for the duration of your loop.

![code snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrmmspq8urfqwsvi64qa.PNG)

![console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur5o3wzm67zwyxvm5346.PNG)

As you can see here, we have our phrase printing to us 10 times, and we are also printing the value of 'i' so that you can visually see that 'i' is increasing in value each time that our loop runs

I mentioned previously that another use of for loops is to loop through an array's values. To do so, all we would need to do is reference the array's length property instead of hardcoding the condition expression to a specific number. We would then be able to use 'i' as the index value, so that each index in our array is utilized.

![code snippet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzdgki571m2aws7nzv0h.PNG)

![console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7if5s5xnrxsx9mvq1jbp.PNG)

When applying these concepts to a real scenario, you likely won't just be printing out values to the console. But now you can take these same concepts of creating a for loop and apply them if you need a function to run a certain amount of times, or if you need access to the index value in your loop.

Let me know how you decide to implement a for loop in your project!