js sequential loop 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: javascript loop through array

const myArray = ['foo', 'bar'];

myArray.forEach(x => console.log(x));

//or

for(let i = 0; i < myArray.length; i++) {
  console.log(myArray[i]);
}

Example 3: js loop through array

// ES6 for-of statement
for (const color of colors){
    console.log(color);
}

// Array.prototype.forEach
const array = ["one", "two", "three"]
array.forEach(function (item, index) {
  console.log(item, index);
});

// Sequential for loop
for (var i = 0; i < arrayLength; i++) {
    console.log(myStringArray[i]);
    //Do something
}