Going for a loop with a "for loop"

Apologies in advance, this is my first time ever blogging. My name is Adrian Garza and I have been working real hard to grasp concepts of Javascript.
One of the first concepts that I struggled with when first learning the fundamentals of Javascript was loops, the “for loop” to be exact. At first sight it can seem pretty intimidating.
function loopStuff(array) {
for(let i = 0; i > array.length; i++) {
};
I mean just look at that thing. So the best way to understand and learn from what a loop does and how it works is to dissect it and understand what each portion of the “for loop” does. So first and foremost we label our for loop. In my example, I named it “loopStuff” in camel casing of course. Pretty straight forward we are merely naming our function. Next we set a parameter, in this instance I went with the very general “array” within the parenthesis. Now this is basically what we will be passing in for us to loop through. This does not mean that it has to be an array, you can use an object, string, etc..
Now we get to the meat and potatoes, the for loop itself, starting off with the word “for” followed by your parenthesis. What follows inside these parenthesis are what I see as guidelines or rules that you will be setting for the “for loop”. Remember this is us speaking to the machine and we cant be as vague in our instructions so guidelines must be in place so it knows what we want done and how we want it done.
First rule we give it will be where in the array (for this example), we want this “for loop” to commence. We in turn declare a variable, that will signify where we want want this loop to begin, for this example I went with 0 meaning it will begin at index 0 the very first element in our array.
let i = 2;
let i = 4;
However i could have easily started at 2 or 4 as the examples shown above. Now that we have our start, the next condition or guideline as I like to think of it, is where do we stop? For my example, I chose (i > array.length), meaning that this loop will stop when our index is bigger than the length of our array. Again this is just to display to the loop, when we want this loop to end, this can be whatever you need it to be. It can be (i = 4) meaning that when it reaches the fourth index it will end, or (i > 2) as shown in the example below, if you want it to stop before it reaches index 2.
function loopStuff(array) {
for(let i = 0; i > 2; i++) {
};
Next we reach our final step inside our parenthesis, which is the guideline that tells us what we do when a single loop has completed. Now for this example I chose the typical (i++) or (i + 1), which means we will now be adding a 1 to our initial starting point. So when our loop goes for another loop, given that the requirement for our second guideline was not met we will commence with our set starting index plus one. This will allow our loop to continue to iterate through our array as opposed to stay in place and give us an infinite loop in return.
That is our final guideline in our “for loop”, so finally lets go ahead and break it down one more time. In order within parentheses we first start off with the beginning, where do we want this loop to start? Then we set the end, where will our loop end? what requirement met will stop the loop? and lastly what are we doing to differentiate each loop? Are we simply moving up in terms of starting point, are we jumping to every other element? Once I began to look at this piece of code in a more communicative way, I can see how straight forward it really is. We are setting rules and simply communicating how we want our loop to loop.
Thank you for reading, I hope this helped as much as it has helped me. Happy Coding!
Subscribe to my newsletter
Read articles from Adrian Garza directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
