HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

JavaScript For Loop

In JavaScript, the for loop is used to iterate over a block of code a specified number of times.

Syntax

    
for (initialization; condition; increment/decrement) {
    // Code to be executed
}

Different Kinds of Loops

  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • for/of - loops through the values of an iterable object
  • while - loops through a block of code while a specified condition is true
  • do/while - also loops through a block of code while a specified condition is true

The For Loop

In JavaScript, the for loop is used to iterate over a block of code a specified number of times.

Example

for (let i = 0; i < 5; i++) {
    console.log(i);
}
You can click on above box to edit the code and run again.

Output

For/In Loops

The JavaScript for in statement loops through the properties of an Object:

Example

for (key in object) {
  // code block to be executed
}    

for/of

The JavaScript for of statement loops through the values of an iterable object.

It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more:

Example

for (variable of iterable) {
  // code block to be executed
}
}

while Loop

In JavaScript, the for loop is used to iterate over a block of code a specified number of times.

Example

for (let i = 0; i < 5; i++) {
    console.log(i);
}
       

The For Loop

In JavaScript, the for loop is used to iterate over a block of code a specified number of times.

Example

for (let i = 0; i < 5; i++) {
    console.log(i);
}
You can click on above box to edit the code and run again.

Output