The forEach()
method is like
a friendly guide that helps us
visit each element in an array
one by one.
It allows us to perform a specific
action on every element without
changing the original array.
Let us look at an example:
In the code above, we define
a function displayFruit
,
which accepts a fruit's name
as a parameter and shows a
message with the fruit's name.
Then, we use the forEach()
method
to call this function on each fruit
in the fruits
array.
This way, we display each fruit with
the same message.
Let's make our code cleaner:
We moved the function definition
within the forEach()
by passing an
anonymous function as the callback
to the forEach()
method.
In the above code, the function we pass
as an argument to forEach()
can receive
three helpful parameters:
element
: This is the current element
of the array that we're visiting.
index
: This is the index position of
the current element in the array.
array
: This is the entire array itself.
Let's use them to get more details about
each fruit in the array: