for(i in array) javascript 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: loop array javascript
var colors = ['red', 'green', 'blue'];
colors.forEach((color, colorIndex) => {
console.log(colorIndex + ". " + color);
});
Example 3: for of array javascript
let colors = ['red', 'green', 'blue'];
for (const color of colors){
console.log(color);
}
Example 4: for array javascript
let iterable = [10, 20, 30];
for (let value of iterable) {
value += 1;
console.log(value);
}