loop array javascript
snippet in javascript

loop through an array javascript

user8793

let array = ['Item 1', 'Item 2', 'Item 3'];

// Here's 4 different ways
for (let index = 0; index < array.length; index++) {
  console.log(array[index]);
}

for (let index in array) {
  console.log(array[index]);
}

for (let value of array) {
  console.log(value); // Will log value in array
}

array.forEach((value, index) => {
  console.log(index); // Will log each index
  console.log(value); // Will log each value
});

how to iterate through a list in javascript

user6150

<script> 
index = 0; 
array = [ 1, 2, 3, 4, 5, 6 ]; 
  
array.forEach(myFunction); 
function myFunction(item, index) 
{ 
    console.log(item); 
}</script>

loop array javascript

user6190

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

javascript iterate array

user8853

var txt = "";
var numbers = [45, 4, 9, 16, 25];

numbers.forEach(function(value, index, array) {
  txt = txt + value + "<br>";
});

js loop array

user9177

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

javascript loop through array

user9118

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