javascript for loop an array code example
Example 1: javascript loop through array
var data = [1, 2, 3, 4, 5, 6];
for(let i=0; i<=data.length; i++) {
console.log(data[i])
}
for(let i of data) {
console.log(i)
}
for(let i in data) {
console.log(i)
console.log(data[i])
}
data.forEach((i) => {
console.log(i)
})
data.map((i) => {
console.log(i)
})
Example 2: js loop
var colors=["red","blue","green"];
for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
Example 3: javascript loop
let array = ['Item 1', 'Item 2', 'Item 3'];
array.forEach(item => {
console.log(item);
});
Example 4: js loop array
let colors = ['red', 'green', 'blue'];
for (const color of colors){
console.log(color);
}
Example 5: javascript loop through array
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));