...for the loop | Dale Greve

…for the loop

The for loop is used to loop through an array or object.

The syntax is similar to a for statement in that it is checks for some type of condition and executes a command. Three pieces separated by semicolons are needed to create the for loop.

  1. The initial value (generally a variable)
  2. Condition when to run the loop
  3. How to change the value each loop

The following code with log “Hello World” to the console 10x and then stops. It sets the a variable (i) with an initial value of 1, then tells the code to run the loop if i is less than or equal to 10, the final piece increases i by 1 each loop.

for (let i = 1; i<= 10, i++) {
console.log("Hello World!")

The for loop is best used when you have a known start and end point. When you have something that has an unkown outcome a while loop would be used.