JavaScript For Loop

Back to JS page

Table of Contents

The For Loop

The for statement creates a loop with 3 optional expressions:

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.

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Loop</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The For Loop</h4>
<p id="demo"></p>

<script>
let text = "";
for (let i = 0; i < 5; i++) {
  text += "Number: " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 1

Result View Example

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:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Loop</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>How to use exp 1</h4>
<p id="demo"></p>

<script>
let i = 0;
let text = "";
for (; i < 5; i++) {
  text += "Number: " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 2

Result View Example

How to use exp 2

exp 2 is used to evaluate the condition of the initial variable. If exp 2 returns false, the loop will end.

If you omit exp 2, you must provide a break inside the loop. Otherwise the loop will never end:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Loop</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>How to use exp 2</h4>
<p id="demo"></p>

<script>
let text = "";
let i = 0;
for (; ; i++) {
  if (i > 4) break;
  text += "Number: " + i + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 3

Result View Example

How to use exp 3

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

exp 3 is optional. It can be omitted if you increment the value inside the loop:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Loop</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>How to use exp 3</h4>
<p id="demo"></p>

<script>
let text = "";
let i = 0;
for (; i < 5; ) {
  text += "Number: " + i + "<br>";
  i++;
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 4

Result View Example

Loop Scope

Using var in a loop: the variable declared in the loop redeclares the variable outside the loop.

Using let in a loop: the variable declared in the loop does not redeclare the variable outside the loop:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript For Loop</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Loop Scope (let)</h4>
<p id="demo1"></p>
<p id="demo2"></p>

<script>
let text = "";
for (let i = 0; i < 5; i++) {
  text += i + " ";
}
document.getElementById("demo1").innerHTML = "Loop values: " + text;
// i is NOT accessible here (let has block scope)
document.getElementById("demo2").innerHTML = "Using let: i is not accessible outside the loop";
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference