JS TutorialLesson #24

JavaScript For Loop

For Loops can execute a block of code a number of times.

For Loops can execute a block of code a number of times.

For Loops are fundamental for tasks like performing an action multiple times.

Share

The For Loop

The for statement creates a loop with 3 optional expressions:

for (exp 1; exp 2; exp 3) { // code block to be executed }

exp 1 is executed (one time) before the execution of the code block.

exp 2 defines the condition for executing the code block.

exp 3 is executed (every time) after the code block has been executed.

Example

Example Code

javascript
Try it Yourself
for (let i = 0; i  ";

}

From the example above, you can read:

exp 1 sets a variable before the loop starts (let i = 0).

exp 2 defines the condition for the loop to run (i must be less than 5).

exp 3 increases a value (i++) after the code block has been executed.


Example

Example Code

javascript
Try it Yourself
const cars = ["BMW", "Volvo", "Saab", "Ford"];

let len = cars.length;

let text = "";

for (let i = 0; i   If you omit exp 2, you must provide a break inside the loop.
> Otherwise the loop will never end.
> This will crash your browser.

---

## How to use exp 3

**exp 3** increments the value of the initial variable (i++).

**exp 3** is optional.

**exp 3** can do anything like negative increment (i--), positive increment (i = i + 15), or anything else.

**exp 3** can be omitted (if you increment the value inside the loop):

### Example

javascript const cars = ["BMW", "Volvo", "Saab", "Ford"];

let len = cars.length;

let i = 0;

let text = "";

for (; i ";

i++; }

Example Code

html
Try it Yourself
---

## Loop Scope

Using `var` in a loop:

### Example

javascript var i = 5;

for (var i = 0; i


Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
for (let i = 0; i < 5; i++) {

   text += "The number is " + i + "<br>";

}

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const cars = ["BMW", "Volvo", "Saab", "Ford"];

let len = cars.length;



let text = "";

for (let i = 0; i < len; i++) {

  text += cars[i];

}

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const cars = ["BMW", "Volvo", "Saab", "Ford"];

let len = cars.length;



let i = 2;



let text = "";

for (; i < len; i++) { 

    text += cars[i] + "<br>";

 }

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
const cars = ["BMW", "Volvo", "Saab", "Ford"];

let len = cars.length;



let i = 0;



let text = "";

for (; i < len; ) { 
  text += cars[i] + "<br>";

   
 i++;
}

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
var i = 5;



for (var i = 0; i < 10; i++) {

  // some code
}



// Here i is 10

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let i = 5;



for (let i = 0; i < 10; i++) {

    // some code
}



// Here i is 5