for loop in JavaScriptThis is poster of for loop in JavaScript

for loop in JavaScript is used to repeat a block of code.

For example, if you want to display a message 100 times, you can use a loop. It’s just a simple example; You can do much more with loops. This article focuses on JavaScript for loop. You’ll learn more about the other types of loops in future articles.

for loop in JavaScript

Syntax of for loop is

// Syntax of for loop

for (initialization; condition; updation) {
    // loop body
}
  1. The initialization part initializes and/or declares variables and is only executed once.
  2. The condition is evaluated.
    • If the condition is false, the for loop ends.
    • If the condition is true, the code block inside the for loop is executed.
  3. The updation part updates the value of initialized expression when the condition is true.
  • The condition is evaluated again. This process continues until the condition is false.

If you want to learn more about conditions visit JavaScript Comparison and Logical operators

Flow Chart of for Loop

Flow Chart of for loop
Flow Chart of JavaScript for loop

 

Example 1: Print the message five times on the console.

// program to print message 5 times on console
let num = 5;

// looping from i = 1 to 5
for (let i = 1; i <= num; i++) {
    console.log("Recursive Minds!");
}

Output:

Recursive Minds!
Recursive Minds!
Recursive Minds!
Recursive Minds!
Recursive Minds!

Let’s discuss how the program works.

1st Iteration:  i = 1 and n=5, So, the condition is i<=n that is true, and it will print “Recursive Minds!” and the value of i increment by 1.

2nd Iteration:  i = 2 and num=5, So, the condition is i<=num that is true, and it will print “Recursive Minds!” and the value of i increment by 1.

3rd Iteration:  i = 3 and num=5, So, the condition is i<=num that is true, and it will print “Recursive Minds!” and the value of i increment by 1.

4th Iteration:  i = 4 and num=5, So, the condition is i<=num that is true, and it will print “Recursive Minds!” and the value of i increment by 1.

5th Iteration:  i = 5 and num=5, So, the condition is i<=num that is true, and it will print “Recursive Minds!” and the value of i increment by 1.

6th Iteration:  i = 6 and num=5, So, the condition is i<=num that is False, and it will terminate the loop and our program complete its execution.

Example 2. Print Numbers from 0 to 5

// program to print numbers from 1 to 5
let n = 5;

// we start i from 1 and it will go upto 5
for (let i = 1; i <= n; i++) {
    console.log(i);     // printing the value of i
}

Output:

1
2
3
4
5

JavaScript Infinite Loop

If the condition inside for loop is always true then it runs forever until the memory is full, this is also called an infinite loop.

Example:

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

In the above code, the condition is always true, this loop will run infinite times.

Thank you so much for reading

Happy Coding

For more details about for loop visit MDN Docs

Some other Articles on JavaScript

Leave a Reply

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