javascript loop thru 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: javascript loop through array
const numbers = [1, 2, 3, 4]
numbers.forEach(number => {
console.log(number);
}
for (let i = 0; i < number.length; i++) {
console.log(numbers[i]);
}
Example 3: loop through javascript array
let colors = ['red', 'green', 'blue'];
for (const color of colors){
console.log(color);
}
Example 4: javascript loop through array
const myArray = ["foo","bar"];
myArray.map(item => console.log(item));
myArray.forEach(item => console.log(x));
for(let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}