for loop in html element javascript code example
Example 1: javascript for loop
var colors=["red","blue","green"];
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
Example 2: javascript loop through array
let my_array = [1, 2, 3, 4, 5];
// standard for loop
for(let i = 0; i < my_array.length; i++) {
console.log(my_array[i]) // 1 2 3 4 5 6
}
// for and of method
for(let i of my_array) {
console.log(i)
}
/*
Results:
1 2 3 4 5
1 2 3 4 5
(From both methods)
*/