cities.forEach(city => console.log(city)); → this line prints out:
Orlando
Dubai
Edinburgh
Chennai
Accra
Denver
Eskisehir
Medellin
Yokohama
How does this method return undefined? Can someone explain that please. I’m new.
As we can see, using the forEach() iterator we are able to act upon each and every element in the array, such as above logging of its value. The actions occur immediately but do not mutate the array. There is nothing to return. Try assigning forEach…
Returning is a necessary part of saving a result for later once the function is complete. .forEach() is not that type of function. Since it is acting on every element, what would we have it return?
Clarity comes from continued study, practice and usage. Give it time. Just don’t expect too much of yourself other than putting in the time and effort. Enjoy the small successes. Relish in confusion and mistakes… They are the fuel we need to compound learning.
It varies, so let’s study the .forEach() iterator as it applies to mapping and filtering (mind those other iterators are lighter weight with smaller overhead).
There are not many instances where we use this in a function, save within methods (functions as object properties). this is the name of the execution context. You’ll have lots of time to study and learn all its workings as you progress.