forEtch js code example
Example 1: js foreach
var stringArray = ["first", "second"];
myArray.forEach((string, index) => {
var msg = "The string: " + string + " is in index of " + index;
console.log(msg);
// Output:
// The string: first is in index of 0
// The string: second is in index of 1
});
Example 2: foreach js
var fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
function myFunction(item, index) {
document.getElementById("demo").innerHTML
+= index + ":" + item + "<br>";
}