Looping in programming languages is a feature that facilitates the execution of a set of instructions repeatedly until some condition evaluates and becomes false. We come across for loop which provides a brief and systematic way of writing the loop structure.
Syntax:
for (statement 1 ; statement 2 ; statement 3)
- Statement 1 is the initialization of the counter. It is executed once before the execution of the code block.
- Statement 2 is the testing statement that defines the condition for executing the code block It must return a boolean value. It is also an entry controlled loop as the condition is checked before the execution of the loop statements.
- Statement 3 is increment or decrement of counter & executed (every time) after the code block has been executed.
<script type = "text/javaScript"> // JavaScript program to illustrate for loop var x; // for loop begins when x=2 // and runs till x <=4 for (x = 2; x <= 4; x++) { document.write("Value of x:" + x + "<br />"); } < /script> |
Output :
Value of x:2 Value of x:3 Value of x:4
Flow chart :

Statement 1
We can initialize the counter variable externally rather than in statement 1. This shows us clearly that statement 1 is optional. We can leave the portion empty with a semicolon. For example:
<script> var x = 2; for ( ; x <= 4; x++) { document.write("Value of x:" + x + "<br />"); } </script> |
Statement 2
This statement checks the boolean value of the testing condition. If the testing condition is true, the for loop will execute further. It is executed every time the for loop runs before the loop enters its body. If this statement returns true, the loop will start over again, otherwise it will end and move over to the code following the loop body. This is also an optional statement and Javascript treats it as true if left blank. If this statement is omitted, the loop runs indefinitely if the loop control isn’t broken manually. It is explained below:
<script> var x = 2; bool for ( ; ; x++) { document.write("Value of x:" + x + "<br />"); break; } </script> |
Statement 3
It is a controlled statement that controls the increment/decrement of the counter variable. It is also optional by nature and can be done inside the loop body. For example:-
<script> var i = 0; var len = 2; var gfg = ""; for (; i < len; ) { gfg += cars[i] + "<br>"; //can be increased inside loop i++; } </script> |
for/in
There is another advanced loop called for/in loop which run through all the properties of an object. The loop will be executed once for each property of the object.
Syntax : for (var in object) { statements to be executed }
Example:
<!DOCTYPE html> <html> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <button onclick="GFG()">Try it</button> <p id="demo"></p> <script> function GFG() { var Platform= {fname:"geeks", Mname:"for", lname:"geeks", }; var text = ""; var x; for (x in Platform) { text += Platform[x] + " "; } document.getElementById("demo").innerHTML = text; } </script> </body> </html> |
Output :

Recommended Posts:
- JavaScript | While Loop
- How to use loop through an array in JavaScript?
- How to break nested for loop using JavaScript?
- How to add a delay in a JavaScript loop?
- How to trigger setInterval loop immediately using JavaScript ?
- How to ignore loop in else condition using JavaScript ?
- JavaScript for-in Loop
- What are the microtask and macrotask within an event loop in JavaScript ?
- JavaScript SyntaxError - A declaration in the head of a for-of loop can't have an initializer
- HTML | DOM Video loop Property
- PHP | Separate odd and even elements from array without using loop
- Node.js REPL (READ, EVAL, PRINT, LOOP)
- R - Repeat loop
- Iterate associative array using foreach loop in PHP
- Why are elementwise additions much faster in separate loops than in a combined loop?
- Printing all subsets of {1,2,3,...n} without using array or loop
- Determine the first and last iteration in a foreach loop in PHP?
- C# | Using foreach loop in arrays
- Create a pandas column using for loop
- How to remove an array element in a foreach loop?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ArkadyutiBanerjee


