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 jus look at that thing. So the best way to understand and learn from what a loop does and how it works was to dissect it and understand what each portion of the “for loop” does. So first and foremost we label our for loo, in my example I named it “loopStuff” in camel casing of course. Pretty straightforward 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 which in 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 so 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) if you want it to stop before it reaches index 2.

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”