Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Dates JS Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Events JS Programming JS References JS UTF-8 Characters JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Promises JS Modules JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript For Loop

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

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

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

for (let i = 0; i < 5; i++) {
  text += "The number is " + i + "<br>";
}
Try it Yourself »

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

Use a for loop to collect the car names from the cars array:

const cars = ["BMW", "Volvo", "Saab", "Ford"];
let len = cars.length;

let text = "";
for (let i = 0; i < len; i++) {
  text += cars[i];
}
Try it Yourself »

How to use exp 1

exp 1 is used to initialize the variable(s) used in the loop (let i = 0).

exp 1 is optional.

You can omit exp 1 if the value is set before the loop starts:

Example

const cars = ["BMW", "Volvo", "Saab", "Ford"];
let len = cars.length;

let i = 2;

let text = "";
for (; i < len; i++) {
  text += cars[i] + "<br>";
}
Try it Yourself »

How to use exp 2

exp 2 is used to evaluate the condition of the initial variable (i < len).

exp 2 is also optional.

If exp 2 returns false, the loop will end.

Note

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

const cars = ["BMW", "Volvo", "Saab", "Ford"];
let len = cars.length;

let i = 0;

let text = "";
for (; i < len; ) {
  text += cars[i] + "<br>";
  i++;
}
Try it Yourself »

Loop Scope

Using var in a loop:

Example

var i = 5;

for (var i = 0; i < 10; i++) {
  // some code
}

// Here i is 10
Try it Yourself »

Using let in a loop:

Example

let i = 5;

for (let i = 0; i < 10; i++) {
  // some code
}

// Here i is 5
Try it Yourself »

In the first example, using var, the variable declared in the loop redeclares the variable outside the loop.

In the second example, using let, the variable declared in the loop does not redeclare the variable outside the loop.

When let is used to declare the i variable in a loop, the i variable will only be visible within the loop.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.