javascript es5 foreach code example
Example 1: js for each item do
let array = ['Item 1', 'Item 2', 'Item 3'];
array.forEach(item => {
console.log(item);
});
Example 2: javascript .foreach
let colors = ['red', 'blue', 'green'];
colors.forEach(function(color, idx, sourceArr) {
console.log(color, idx, sourceArr)
});
Example 3: forEach
const arraySparse = [1,3,,7]
let numCallbackRuns = 0
arraySparse.forEach((element) => {
console.log(element)
numCallbackRuns++
})
console.log("numCallbackRuns: ", numCallbackRuns)