Javascript For Loop

In this tutorial, you will learn about javascript for loop with the help of multiple examples.

In programming languages, loops are used to execute the same block of code a certain number of times.

For example, if you want to print “Hello, World!” 100 times, then in this situation you can use a loop. it is just a simple example to explain the loops; you can achieve a lot more with loops.

Here we are only going to focus on for loop. We will cover other types of loops in the next tutorials.

JavaScript for loop

for keyword is used to create a for loop that consists of three optional expressions, enclosed in parenthesis () and separated by semicolons ;, followed by a block of code to be executed in the loop.

The syntax of the for loop is:

for (initialization; condition; increment) {
    // for loop body
}

Here,

Initialization: It is an expression or variable declaration evaluated once before the loop begins. It is usually used to initialize a counter variable. For example, let i = 0.

condition: It specifies a condition to be evaluated before each loop iteration. If the condition evaluates to true, the block of code inside for loop is executed. Else, If the condition is evaluated to false, the for loop is terminated.

increment: It is an expression that evaluates at the end of each loop iteration. It is usually used to update or increment the counter variable.

In loops, we use comparison and logical operators to specify conditions. To learn more about these operators visit Comparison and Logical Operators in Javascript

Example 1: Print Text Multiple Times

//program to print text 5 times
const n = 5;
//looping from i = 0 to 5
for (let i = 1; i <= n; i++) {
    console.log("Hello,World!");
}

Output

Hello,World!
Hello,World!
Hello,World!
Hello,World!
Hello,World!

Here is the working of the above program.

IterationVariableCondition: i <= nAction
1stlet i = 1;
n = 5;
trueHello,World! is printed.
i is increased to 2.
2ndlet i = 2;
n = 5;
trueHello,World! is printed.
i is increased to 3.
3rdlet i = 3;
n = 5;
trueHello,World! is printed.
i is increased to 4.
4thlet i = 4;
n = 5;
trueHello,World! is printed.
i is increased to 5.
5thlet i = 5;
n = 5;
trueHello,World! is printed.
i is increased to 6.
6thlet i = 6;
n = 5;
falseLoop is terminated

Example 2: Print 1 to 10

//program to print numbers from 1 to 10
const n = 10;
//looping from i = 0 to 10
//i is increased by 1  in each iteration
for(let i = 1; i<=n; i++){
    console.log(i);
}

Output

1
2
3
4
5
6
7
8
9
10

Here is the working of the above program.

IterationVariableCondition: i <= nAction
1stlet i = 1;
n = 10;
true1 Is printed.
i is increased to 2.
2ndlet i = 2;
n = 10;
true2 Is printed.
i is increased to 3.
3rdlet i = 3;
n = 10;
true3 Is printed.
i is increased to 4.
4thlet i = 4;
n = 10;
true4 Is printed.
i is increased to 5.
5thlet i = 5;
n = 10;
true5 Is printed.
i is increased to 6.
6thlet i = 6;
n = 10;
true6 Is printed.
i is increased to 7.
7thlet i = 7;
n = 10;
true7 Is printed.
i is increased to 8.
8thlet i = 8;
n = 10;
true8 Is printed.
i is increased to 9.
9thlet i = 9;
n = 10;
true9 Is printed.
i is increased to 10.
10thlet i = 10;
n = 10;
true10 Is printed.
i is increased to 11.
11thlet i = 11;
n = 10;
falseLoop is terminated

Example 3: Print Sum of n Natural Numbers

//program to print sum of n natural numbers
const n = 100;
let sum = 0;
//looping from i = 0 to 10
//i is increased by 1 in each iteration
for (let i = 1; i <= n; i++) {
    sum += i;
}

console.log(`The Sum is: ${sum}`);

Output

The sum is: 5050

In the above program, we are using for loop to add the numbers from 1 to 100, for that, we are using a variable sum which is initially initialized to 0.

On each iteration, i is added to the sum and its value is increased by 1. This process will continue until the value of i becomes 101 and the test condition becomes false.

And when the loop terminates we get the sum which is equal to 0 + 1 + 2 + … + 100;

Example 4: Print Even Numbers

//program to print even numbers
const n = 10;
//looping from i = 0 to 10
//i is increased by 1 in each iteration
for (let i = 1; i <= n; i++) {
    if (i % 2 == 0) {
        console.log(i);
    }
}

Output

2
4
6
8
10

In the above program, we are looking for even numbers in the range 1 to 10. We have used if statement inside the for loop with the condition i % 2 == 0.

On each iteration, the if condition will check the value of i, if the value is even it will be displayed to the console, and, if the value is odd, the body of the if statement will be skipped from execution

Infinite for loop in Javascript

In an infinite for loop, the test condition is always true. As its name suggests infinite loop runs forever. For example,

// infinite for loop
for(let i = 1; i > 0; i++) {
    // code
}

In the above example, the condition i > 0 always evaluates to true which causes your program to run an infinite number of times.

Leave a Comment

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