FAQ: Iterators - The .forEach() Method

Callbacks have been massively under explained in this course and I’m not understanding this either because of it (quite frustrating).

Can someone explain where ‘groceryItem’ comes from for this part?

I understand that it’s individual indexes of the array but what is telling the computer that?

Shouldn’t it be groceries [i] or something?

groceryItem is not an index, its a value from the array.

simply said, .forEach loops overs the array, and at each iteration calls your function, passing the current value to the parameter of your function.

I thought an index was a value from the array? very confusing.

Where does groceryItem come from then? What defines it, how does the computer know what it is?

no, the index is the position of the value in the array.

so for example:

const = ['a', 'b', 'c']

a is the value at index 0, b is the value at index 1 and so forth

Are you familiar with functions, parameters and arguments? groceryItem is a parameter, which gets its value from argument at function call. The call is done by .forEach method.

1 Like