looping through arrays javascript code example

Example 1: javascript loop through array

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

Example 2: iterate through array js

let arbitraryArr = [1, 2, 3];
// below I choose let, but var and const can also be used 
for (let arbitraryElementName of arbitraryArr) {
  console.log(arbitraryElementName);
}

Example 3: looping in javascript

for (initialization; test condition; iteration statement) {
   Statement(s) to be executed if test condition is true
}