forEach()

https://www.codecademy.com/courses/introduction-to-javascript/lessons/javascript-iterators/exercises/choose-iterator

const cities = [‘Orlando’, ‘Dubai’, ‘Edinburgh’, ‘Chennai’, ‘Accra’, ‘Denver’, ‘Eskisehir’, ‘Medellin’, ‘Yokohama’];

const nums = [1, 50, 75, 200, 350, 525, 1000];

// Choose a method that will return undefined

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…

cityList = cities.forEach(city => console.log(city))

console.log(cityList)    //  undefined

That isn’t to say that we cannot mutate the array, which in fact we can…

cities.forEach((city, index) => {
  cities[index] = city.toUpperCase()
  console.log(city)
})
​ Orlando
​ Dubai
​ Edinburgh
​ Chennai
​ Accra
​ Denver
​ Eskisehir
​ Medellin
​ Yokohama
cities.forEach(city => console.log(city))
​ ORLANDO
​ DUBAI
​ EDINBURGH
​ CHENNAI
​ ACCRA
​ DENVER
​ ESKISEHIR
​ MEDELLIN
​ YOKOHAMA
1 Like

Okay okay, I think I get it. Would it be fair to say, returning is like saving something for later instead of acting right away?

1 Like

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?

1 Like

I gotcha. This area of Javascript is still a lil fuzzy to me. I’m looking forward to the day I get a firm grasp of it. If that happens lol.

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.

1 Like

Can you explain how the ‘this’ keyword is use in a function?

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).

cities = ['Orlando', 'Dubai', 'Edinburgh', 'Chennai', 'Accra', 'Denver', 'Eskisehir', 'Medellin', 'Yokohama']
lowerAlpha = []

cities.forEach(function (city) {
    if (city.charAt(0) < 'N') {
        this.push(city.toUpperCase())
    }
}, lowerAlpha)

console.log(lowerAlpha)
// ['DUBAI', 'EDINBURGH', 'CHENNAI', 'ACCRA', 'DENVER', 'ESKISEHIR', 'MEDELLIN']

In the above, this is the array, lowerAlpha.

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.

1 Like

Cool, tyvm. I gota take a break for a bit, but I’m gona keep working on these iterators.

1 Like