Answers for "array looping in javascript"

11

javascript iterate array

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 each value
}

array.forEach((value, index) => {
  console.log(index); // Will log each index
  console.log(value); // Will log each value
});
Posted by: Guest on March-26-2020
48

javascript code to loop through array

var colors = ["red","blue","green"];
for (var i = 0; i < colors.length; i++) {
    console.log(colors[i]);
}
Posted by: Guest on July-22-2019
13

loop through an array javascript

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
});
Posted by: Guest on March-25-2020
8

loop array javascript

var colors = ['red', 'green', 'blue'];
	
	colors.forEach((color, colorIndex) => {
     console.log(colorIndex + ". " + color); 
    });
Posted by: Guest on April-08-2020
11

javascript iterate array

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

numbers.forEach(function(value, index, array) {
  txt = txt + value + "<br>";
});
Posted by: Guest on November-25-2019
0

javascript loop and array

var array = ["hello","world"];
array.forEach(item=>{
	console.log(item);
});
Posted by: Guest on June-27-2020

Code answers related to "array looping in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language