for loop in JavaScript

for loop in JavaScript

In this article, we will learn about for loop in JavaScript with the help of examples.

Loops are used in programming to repeat a block of code.

For example: If we want to show a message 100 times, we can use a loop. It’s just a simple example, loops can accomplish a lot more.

JavaScript for loop

The for loop has the following syntax:

for (initialExpression; condition; updateExpression) {
// for loop body
}

Here,

The initialExpression is a single-use expression that declares and initializes variables.

The situation is assessed.

  • The for loop is terminated, if the condition is false.
  • The block of code inside the for loop is executed, if the condition is true.

When the condition is true, the updateExpression changes the value of initialExpression.

The situation is assessed once more. This process is repeated until the condition is no longer true.

Example 1: Display a Text five times using for loop

const n = 5;

// looping from i = 1 to 5
for (let i = 1; i <= n; i++) {
console.log(‘Developers Dome’);
}

Output:

Developers Dome
Developers Dome
Developers Dome
Developers Dome
Developers Dome

Let’s see how this program works:

IterationVariableCondition: i <= nAction
1sti = 1
n = 5
trueDevelopers Dome is printed.
i is increased to 2.
2ndi = 2
n = 5
trueDevelopers Dome is printed.
i is increased to 3.
3rdi = 3
n = 5
trueDevelopers Dome is printed.
i is increased to 4.
4thi = 4
n = 5
trueDevelopers Dome is printed.
i is increased to 5.
5thi = 5
n = 5
trueDevelopers Dome is printed.
i is increased to 6.
6thi = 6
n = 5
falseThe loop is terminated.

Example 2: Display numbers from 1 to 5 using for loop

const n = 5;

// looping from i = 1 to 5
// in each iteration, i is increased by 1
for (let i = 1; i <= n; i++) {
console.log(i); // printing the value of i
}

Output:

1
2
3
4
5

Let’s see how this program works:

IterationVariableCondition: i <= nAction
1sti = 1
n = 5
true1 is printed.
i is increased to 2.
2ndi = 2
n = 5
true2 is printed.
i is increased to 3.
3rdi = 3
n = 5
true3 is printed.
i is increased to 4.
4thi = 4
n = 5
true4 is printed.
i is increased to 5.
5thi = 5
n = 5
true5 is printed.
i is increased to 6.
6thi = 6
n = 5
falseThe loop is terminated.

Example 3: Display sum of n natural numbers using for loop

let sum = 0;
const n = 100

// looping from i = 1 to n
// in each iteration, i is increased by 1
for (let i = 1; i <= n; i++) {
sum += i; // sum = sum + i
}

console.log(‘sum:’, sum);

Output:

sum = 5050

The initial value of the sum is 0 in this case. Then, from i = 1 to 100, a for loop is iterated. i is added to sum and its value is increased by 1 in each iteration.

The test condition is false when i reaches 101, and the sum equals 0 + 1 + 2 +… + 100.

The program to add the sum of natural numbers described above can also be written as:

let sum = 0;
const n = 100

// looping from i = n to 1
// in each iteration, i is decreased by 1
for(let i = n; i >= 1; i– ) {
// adding i to sum in each iteration
sum += i; // sum = sum + i
}
console.log(‘sum:’, sum);

This program generates the same results as Example 3. Programming is all about logic, so you can accomplish the same task in a variety of ways.

Despite the fact that both approaches are correct, you should strive to make your code more readable.

JavaScript Infinite for loop

A for loop runs infinitely, if the test condition is always true (until memory is full). For example:

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

The condition in the above program is always true, causing the code to run infinitely.

That’s all about the for loop in JavaScript.

You may like if-else Statements in JavaScript.

Hope this article will guide you to recognize for loop in JavaScript that you needed and still if you have any problems or queries regarding this, post them in the comments section and we will be glad to assist you.

This Post Has One Comment

Leave a Reply