iterating through 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: javascript iterate array
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(function(value, index, array) {
txt = txt + value + "<br>";
});
Example 3: iterate array javascript
array = [ 1, 2, 3, 4, 5, 6 ];
for (index = 0; index < array.length; index++) {
console.log(array[index]);
}
Example 4: javascript loop through array
let array = ['Item 1', 'Item 2', 'Item 3'];
for (let item of array) {
console.log(item);
}
Example 5: loop through array javascript
const cities = ["Chicago", "New York", "Los Angeles"];
cities.map(city => {
console.log(city)
})
Example 6: iterate over array javascript
var txt = "";
var numbers = [45, 4, 9, 16, 25];
numbers.forEach(myFunction);
function myFunction(value, index, array) {
txt = txt + value + "<br>";
}