loop javascript array code example

Example 1: loop array javascript

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

Example 2: iterate array javascript

array = [ 1, 2, 3, 4, 5, 6 ]; 
for (index = 0; index < array.length; index++) { 
    console.log(array[index]); 
}

Example 3: loop an array in javascript

let array = ["loop", "this", "array"]; // input array variable
for (let i = 0; i < array.length; i++) { // iteration over input
	console.log(array[i]); // logs the elements from the current input
}

Example 4: loop through javascript array

let colors = ['red', 'green', 'blue'];
for (const color of colors){
    console.log(color);
}

Example 5: loop array in javascript

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

Example 6: javascript loop and array

var array = ["hello","world"];
array.forEach(item=>{
	console.log(item);
});