js loop for array code example

Example 1: js for loop

var i; //defines i
for (i = 0; i < 5; i++) { //starts loop
  console.log("The Number Is: " + i); //What ever you want
}; //ends loop
//Or:
console.log("The Number Is: " + 0);
console.log("The Number Is: " + 1);
console.log("The Number Is: " + 2);
console.log("The Number Is: " + 3);
console.log("The Number Is: " + 4);
//They do the same thing!
//Hope I helped!

Example 2: javascript loop through array

let my_array = [1, 2, 3, 4, 5];

// standard for loop
for(let i = 0; i < my_array.length; i++) {
  console.log(my_array[i])  // 1 2 3 4 5 6
}

// for and of method
for(let i of my_array) {
	console.log(i)
}

/*
Results:
1 2 3 4 5
1 2 3 4 5
(From both methods)
*/