Loops are used to repeat things.

Say you want to count from number 0 to 10 in JavaScript. You could manually console.log each number. So it becomes:

console.log (0);

console.log (1);

console.log (2);

console.log (3);

console.log (4); for loops for loops for loops for loops for loops

console.log (5);

console.log (6);

console.log (7);

console.log (8);

console.log (9);

console.log (10);

If we were to run this, we would get 0 to 10 printed in the console. But, this is inefficient and not convenient at all. Now, imagine having to count from 0 to a million! That’s a lot of manual work!

That is where the “for” loop comes in handy.

There are two main loops types: for loops and while loops. In this article, we will focus on the former.

Using a For Loop

A for Loop is a looping statement that allows us to traverse through a list of items and perform an action for each item.

It’s named “for’ because it runs for a “for”-mulated number of times.

A typical for loop syntax looks something like this —

for (let i = 0; i < 10; i++){ 

  // code block to be executed

}

It has three parts:

  1. The initializer – let i = 0: This is where we initialize our counter (where do you want your loop to start)
  2. Our condition – i < 10: This sets the condition when to stop looping (where do you want your loop to end?).
  3. The iterator – i++: This increments the counter after each iteration (do you want your loop to go down or up, and by how much?)

Now let’s use this loop to print 1 to 10 in console.log as we did before without a for loop

for (var i = 0; i < 10; i++) { 

    console.log(i);

}

Pretty neat, isn’t it? We achieved the same thing as before with just one line of code. That’s why for loops are extremely useful.

A for loop has three statements – the initializer, condition, and iterator. It’s like saying, I’m starting a loop at 0, I’m going to keep looping until it reaches 10, and after each iteration, I’m increasing it by 1.

For (var i=0); i<10; 1+=1) {

Console.log(i);

{

The first statement, i = 0, initializes the loop. It’s simply telling the computer to start counting the loop from 0. Note that we can change the 0 to any number less than 10 (1 <10), and the loop will start from the specified count).

The second statement sets the conditions. It simply tells the computer when to stop. In other words, it’s telling the computer not to exceed 10.

The last statement tells the computers the changes to make to each value after the iteration. Should it add or minus, and by how many?

i =+1 is the short form for i= i+1, which can be further shortened to i ++

So, it starts with the first i, which is 0 for loops

Then does some calculations (I = 0 +1)

Now i becomes 1, so it works out the calculations again to come up with the third iteration (i= 1+1), which is 2…. And so on and so on, until the condition stops it.

If you want it to increase by two, all you need to do is change the iterations statement to (i+=2)…

And if you want it to decrease instead, change it to (i-=1)

Note: we can change our variables to anything. It doesn’t have to be necessarily i.

For (var sss = 0; sss <10; sss++) {

Console.log(sss);

}

We can even change where the loop starts — say from 0 to 7, and even change where it ends (from 10 to 50), or change the increments (so it increases by 5 instead of 1:

for (var sss= 7; sss <+50; sss +=5){ for loops

console.log(sss);

}

Much of what we’ve been doing to this point is learning how the for loops works. But how do we use it in practical applications? Let’s look at one example.

for (var num = 0; num<=10; num++){

console.log(num);

}

This will print out numbers 1 to 10

Now let’s say we want to add the numbers (from 1 to 10).

We can start by creating another variable outside the loop (and call it anything). In our case, let’s call it “results:

var result = 0

for (var num=1; num<=10; num++){

result +=num;

}

console.log(result); // 55

The variable we’ve created will be our collector for each number we add to the loop. It will start at zero, adding every iterated number and ending with the result.

We can even add a string to the equation.

var result = 0;

for (var num=1; num<=10; num++){ for loops

console.log(num);

}

console.log(“The result is:” + result);

The result will print out 0, and that’s because we haven’t done anything to it.

var result = 0; for loops

for (var num=1; num<=10; num++){

console.log(num);

result = result +num;

}

console.log(“The result is:” + result);

Now the variable “result” will have the answer: 55.

How this works:

The var result starts at 0, and then the for loop iterates through numbers 1 to 10, one at a time. At the same time, it adds each number to the result (0+1=1; 1+2=3; 3+3=6; 6+4=10 and so on, until it reaches 10, bringing the total result to 55).

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.