how to loop an array with number in javascript 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: how to cycle through an array js

// Requiring the lodash library 
// src: https://lodash.com/docs/4.17.15#forEach

// console:  npm i --save lodash
const _ = require("lodash")

// Use of _.forEach() method

_.forEach([1, 2], function(value) {
  console.log(value)
});
// => Logs `1` then `2`.

// traversing an object
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
  console.log(key)
})
// => Logs 'a' then 'b' (iteration order is not guaranteed).

//////////////// To not use lodash ////////////////
// src: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

function logArrayElements(element, index, array) {
    console.log("a[" + index + "] = " + element)
}
[2, 5, 9].forEach(logArrayElements)
// logs:
// a[0] = 2
// a[1] = 5
// a[2] = 9