how to iterate in javascript code example
Example 1: javascript loop through array
var colors = ["red","blue","green"];
colors.forEach(function(color) {
console.log(color);
});
Example 2: loop an array in javascript
let array = ["loop", "this", "array"];
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
Example 3: iterate array in javascrpt
let array = [ 1, 2, 3, 4 ];
for( let element of array ) {
console.log(element);
}
Example 4: Iterate with JavaScript For Loops
var myArray = [];
for (var i = 1; i <= 5; i++){
myArray.push(i);
}
console.log(myArray)