Why does Choose the Right Iterator say that the .forEach iterator will return undefined when it prints every line perfectly?

Code in the main.js file:

const cities = [‘Orlando’, ‘Dubai’, ‘Edinburgh’, ‘Chennai’, ‘Accra’, ‘Denver’, ‘Eskisehir’, ‘Medellin’, ‘Yokohama’];
// Choose a method that will return undefined
cities.forEach(city => console.log('Have you visited ’ + city + ‘?’));

The line said cities.method(city…); I had to replace “method” with “forEach” to get the right answer; yet, in the console, I have:

Have you visited Orlando?
Have you visited Dubai?
Have you visited Edinburgh?
Have you visited Chennai?
Have you visited Accra?
Have you visited Denver?
Have you visited Eskisehir?
Have you visited Medellin?
Have you visited Yokohama?

Can someone please explain how this is recognized as undefined? Is this just an error on Codecademy’s behalf? I’ve seen the instances where they want to show you what not to do and purposely have you write code that will literally print ‘undefined’ in the console when you run it. This doesn’t make any sense to me.

Welcome to the forums!

So, I believe it is undefined because the callback function passed into .forEach never returns a value, (this is the case for any call to console.log, it will never return anything, hence is undefined, even though it displays a value to the console). :slight_smile:

That said, I’m not entirely sure on the instructions for the exercise task, would you mind dropping a link to that exercise so I can take a look to confirm and make sure I’m not misunderstanding anything?

1 Like

Even if the callback function would return anything, the forEach method doesn’t.
The logs are directly printed from the function body and not returned.

Example

const arr = ['Reykjavik', 'Oslo', 'Stockholm']
const value = arr.forEach(el => `I would like to visit ${el}`)
console.log(value) // undefined
const arr = ['Reykjavik', 'Oslo', 'Stockholm']
const value = arr.map(el => `I would like to visit ${el}`)
console.log(value) // ["I would like to visit Reykjavik", "I would like to visit Oslo", "I would like to visit Stockholm"]
2 Likes

Thank you for taking the time to respond. So from what I understand, it is because the .forEach iterator is incapable of returning anything, which is why we have to use console.log to get something in the console. A return line will work for the other iterators and don’t need a console.log to print to the console. Is this correct?

1 Like

Thank you for responding. I think I understand it now but here is the link:
https://www.codecademy.com/courses/introduction-to-javascript/lessons/javascript-iterators/exercises/choose-iterator

1 Like

Yes, the forEach method is the only iterator I can think of that doesn’t have a return value.
map() and forEach() are the most similar methods, I would say. But they have different use cases. If you want to turn one string into another matching certain criteria, map() is probably the most efficient way. But there are cases where you need forEach, for example if you want to add properties to an existing object for each value in an array.

2 Likes