---
title: "The for loop in JavaScript"  
description: "The for loop in JavaScript"  
author: "Danish Khan"  
published: 2012-10-08  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1037/the-for-loop-in-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 3 minutes  

---

# The for loop in JavaScript

[Introduction](https://yourviews.mindstick.com/view/85450/mesopotamia-introduction-to-an-ancient-civilization):\

The [for loop](https://www.mindstick.com/forum/857/nested-for-loop-in-java-for-a-triangle) is very popular looping constructs and it is widely used also when we want [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) same block of code over and over again.

##### Syntax of For Loop

```
for (Initialization; condition; Iteration statement)  { code to be executed   //if the condition is true  }
```

##### It has three parts:

1) [Initialization](https://www.mindstick.com/interview/608/what-is-the-difference-between-assignment-and-initialization) Statement

2) [Condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition) Statement

3) Iteration Statement

In Initialization Statement we initialize our [counter](https://www.mindstick.com/interview/33859/how-to-create-pixel-counter-in-javascipt) [variable](https://www.mindstick.com/blog/11493/what-is-a-variable) for eg. i=0; The initialization statement is the first statement and it is executed before the code given inside the loop is executed.

Condition Statement or test statement specifies the test criteria for the body part to keep executing. Till the time the Test Statement is true loop will continue executing as soon as the condition becomes false the code which are written inside the loop [stops working](https://answers.mindstick.com/qa/50437/how-to-fix-messenger-that-keeps-crashing-or-stops-working-on-your-iphone-x-after-installing-a-new-ios-update).

Iteration Statement iterates or decrements the counter variable by a specific number for eg. (i++ or i--). Each time our loop executes it either increments it or decrements it as the [requirement](https://yourviews.mindstick.com/view/85169/becoming-an-influencer-requirement-of-skills-and-knowledge) arrises.

##### Example of For loop:

```
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        function forloop() {
            var a;
            document.write("For loop started" + "<br />");
            for (a = 0; a <= 5; a++) {
                document.write("a's value is" + a);
                document.write("<br />");
            }
            document.write("For loop exited");
        }
    </script>
</head>
<body>
    <div>
        <input id="btnLoop" type="button" value="button" onclick="forloop()" />
    </div>
</body>
</html>
```

In this example, the first [section](https://yourviews.mindstick.com/story/2851/is-c-section-bad) initializes variable a with a value 0, Second part of the for loop is to check the condition i.e. (a<=5), so till the condition satisfies a is less than or equal to 5 the loop will execute and as soon as a’s value reaches more than 5 then the condition becomes false and the loop will exit.

##### Output of the above Program:

##### \
![The for loop in JavaScript](https://www.mindstick.com/mindstickarticle/f6617d39-7441-4e1a-a914-f8884f14081b/images/e44ca1dc-463e-4a44-a2ec-75a1e016f8f2.png)

##### Conclusion:

Through this article we came to know the working of for loop, how to initialize it and check condition. For loop is mostly used in all programming language because it is simple and easy to use.

\

---

Original Source: https://www.mindstick.com/articles/1037/the-for-loop-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
