for each in array js code example
Example 1: javascript foreach
const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
console.log(index, item)
})
Example 2: javascript foreach
var colors = ['red', 'blue', 'green'];
colors.forEach(function(color) {
console.log(color);
});
Example 3: foreach javascript
let words = ['one', 'two', 'three', 'four'];
words.forEach((word) => {
console.log(word);
});
Example 4: js for each item do
let array = ['Item 1', 'Item 2', 'Item 3'];
array.forEach(item => {
console.log(item);
});
Example 5: for each array javascript
var fruits = ["apple", "orange", "cherry"];
fruits.forEach(getArrayValues);
function getArrayValues(item, index) {
console.log( index + ":" + item);
}
Example 6: foreach javascript
let names = ['josh', 'joe', 'ben', 'dylan'];
names.forEach((name, index, sourceArr) => {
console.log(color, idx, sourceArr)
});