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)
})
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
// Iterate over fruits below
// Normal way
fruits.forEach(function(fruit){
console.log('I want to eat a ' + fruit)
});
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
});
var array = ["a","b","c"];
// example 1
for(var value of array){
console.log(value);
value += 1;
}
// example 2
array.forEach((item, index)=>{
console.log(index, item)
})
// array for the forEach method
let grades = [13, 12, 14, 15]
// for each loop
grades.forEach(function(grade) {
console.log(grade) // loops and logs every grade of the grades array
})