Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Expression 1

Expression 1 typically initializes the loop variable (e.g., let i = 0) but is optional in JavaScript and can set multiple values using commas.

Example

for (let i = 0, len = cars.length, text = “”; i < len; i++) {
  text += cars[i] + “<br>”;
}

You can also omit expression 1 if your values are initialized before the loop begins.

Example

let i = 2;
let len = cars.length;
let text = “”;
for (; i < len; i++) {
  text += cars[i] + “<br>”;
}

Expression 2

Expression 2 usually checks the loop condition but is optional in JavaScript; the loop continues if true and stops if false.

If you omit expression 2, you need to include a break statement inside the loop; otherwise, the loop will run indefinitely, potentially crashing your browser. You can learn more about breaks in a later chapter of this tutorial.

Expression 3

Expression 3 typically updates the loop variable but is optional in JavaScript and can perform actions like incrementing (i++), decrementing (i--), or other operations.

You can also omit expression 3 if you’re modifying your values within the loop.

Example

let i = 0;
let len = cars.length;
let text = “”;
for (; i < len; ) {
  text += cars[i] + “<br>”;
  i++;
}