Open In App
Related Articles

JavaScript For Loop

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.

JavaScript for loop is used to iterate the elements for a fixed number of times. JavaScript for loop is used if the number of the iteration is known.

Syntax

for (statement 1 ; statement 2 ; statement 3){
code here...
}
  • Statement 1: It is the initialization of the counter. It is executed once before the execution of the code block.
  • Statement 2: It defines the testing condition for executing the code block
  • Statement 3: It is the increment or decrement of the counter & executed (every time) after the code block has been executed.

Example: 

javascript

// JavaScript program to illustrate for loop
let x;
 
// for loop begins when x=2
// and runs till x <=4
for (x = 2; x <= 4; x++) {
    console.log("Value of x:" + x);
}

                    

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. 

Example: 

javascript

let x = 2;
 
for (; x <= 4; x++) {
    console.log("Value of x:" + x);
}

                    

Output
Value of x:2
Value of x:3
Value of x:4


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

Example: 

Javascript

let x = 2;
for (; ; x++) {
    console.log("Value of x:" + x);
    break;
}

                    

Output
Value of x:2


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.

Example: 

Javascript

const subjects = ["Maths", "Science", "Polity", "History"];
let i = 0;
let len = subjects.length;
let gfg = "";
for (; i < len;) {
    gfg += subjects[i];
    //can be increased inside loop
    i++;
}
console.log(gfg)

                    

Output
MathsSciencePolityHistory


for/in loop: There is another advanced loop called for/in loop which runs through all the properties of an object. The loop will be executed once for each property of the object.

Syntax:

 for (let in object) { statements to be executed } 

Example: 

Javascript

function GFG() {
    let Platform = { fname: "geeks", Mname: "for", lname: "geeks", };
 
    let text = "";
    let x;
    for (x in Platform) {
        text += Platform[x] + " ";
    }
    console.log(text);
}
GFG()

                    

Output
geeks for geeks 


The for/in loop can also be used over the properties of arrays. However, it is not advised to use for/in loop over arrays.  for/of and Array.forEach() loops are suggested to be used while working with arrays.

Syntax: 

for(let in array){statements to be executed}

Example: 

Javascript

const arr = [23, 54, 46, 3];
 
let gfg = "";
for (let i in arr) {
    gfg += arr[i] + '\n';
}
console.log(gfg)

                    

Output
23
54
46
3



We have a Cheat Sheet on JavaScript where we covered all the important topics of JavaScript to check those please go through JavaScript Cheat Sheet – A Basic Guide to JavaScript.



Last Updated : 20 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads