for loops arrays javascript code example

Example 1: loop array javascript

var colors = ['red', 'green', 'blue'];
	
	colors.forEach((color, colorIndex) => {
     console.log(colorIndex + ". " + color); 
    });

Example 2: javascript loop through array

var myStringArray = ["hey","World"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
    console.log(myStringArray[i]);
    //Do something
}

Example 3: javascript for loop

var colors=["red","blue","green"];
for (let i = 0; i < colors.length; i++) { 
  console.log(colors[i]);
}

Example 4: for loop js

let arr = [];
for(let i = 0; i <= 10; i++){
arr[i] = Math.round(Math.random() * 10);
}Result: [3, 1, 1, 2, 3, 9, 5, 6, 6, 8, 5]Or
let arr = [];
for(let i = 0; i <= 10; i++){
arr[i] = Math.trunc(Math.random() * 10);
} 
Result[7, 6, 0, 2, 4, 8, 7, 5, 5, 6, 5]