The forEach()
method is used to call a function that iterates elements in an array or classList. In other words it adds a function for each element in an array or with the selected class.
let students = ['Cady', 'Cam', 'Lens'];
// using forEach
students.forEach(myFunction);
function myFunction(item) {
console.log('Hello ' + item);
}
//The output is all of the arrays items seperately because it runs FOR EACH of the elements and not all at once.
// with arrow function and callback
const students = ['Cady', 'Cam', 'Lens'];
students.forEach(element => {
console.log(element);
});