let arr = [1,2,3]
// forEach accepts a function, and each value in array is passed to the
// function. You define the function as you would any regular
// function, you're just doing it inside the forEach loop
arr.forEach(function(value){
console.log(value)
})
// you also have access to the index and original array:
arr.forEach(function(value, idx, array){
console.log(idx, value, array)
})
// it's common for the arrow notation to be used: it's effectively
// the same thing but the function keyword is removed and the syntax is
// a bit different
arr.forEach((value) => {
console.log(value)
})
Array.from(document.querySelectorAll(".ClassName")).forEach(function(element) {
//Stuff
});
// foreach method of array in javascript
let numberArray = [1,2,3,4,5];
function printArrayValue(value, index){
console.log(`value: ${value} = index: ${index}`);
}
numberArray.forEach(printArrayValue);
// Output:
// value: 1 = index: 0
// value: 2 = index: 1
// value: 3 = index: 2
// value: 4 = index: 3
// value: 5 = index: 4