How to use the foreach() method in JavaScript?

The forEach() method in JavaScript is used to iterate over an array and execute a callback function for each element. The callback function takes in three arguments: the current element, its index, and the array being iterated over.

Here is an example of how to use the forEach() method to log each element in an array:

const arr = [1, 2, 3, 4, 5];

arr.forEach(function(element, index, array) {
  console.log(element);
});

You can also use arrow function for callback and it will look like this:

arr.forEach((element, index, array) => {
    console.log(element);
});

This will log the numbers 1 through 5 to the console.

You can also use the forEach() method with a variable instead of a function. Here is an example:

const print = function(element, index, array) {
  console.log(element);
}
arr.forEach(print);