for loop in javascript for array code example
Example 1: javascript code to loop through array
var colors = ["red","blue","green"];
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
Example 2: loop array javascript
var colors = ['red', 'green', 'blue'];
colors.forEach((color, colorIndex) => {
console.log(colorIndex + ". " + color);
});
Example 3: javascript loop through array
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
Example 4: javascript iterate through for loop
var myStringArray = ["Hello","World"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
console.log(myStringArray[i]);
}
Example 5: javascript loop and array
var array = ["hello","world"];
array.forEach(item=>{
console.log(item);
});
Example 6: javascript array loop
for (let step = 0; step < 5; step++) {
console.log('Walking east one step');
}