What does it mean when it says .forEach returns undefined?


#1

Im not sure what it means when it says “In the previous exercise, we called the .forEach() method and learned that it returns undefined.” When I run:

let fruits = ['mango', 'papaya', 'pineapple', 'apple'];

// Iterate over fruits below

fruits.forEach(function(fruitItem) {
  console.log("I want to eat a" + fruitItem);

it returns:
I want to eat amango
I want to eat apapaya
I want to eat apineapple
I want to eat aapple

That doesn’t look undefined to me, but then again im a total noob. Could someone please explain to me how .ForEach() returns undefined?


#2

At the end, there is no } ) ; symbols to “show” the end of the code. This means that it will show up as undefined, because the code “has no end”.

I guess you can go ahead and at the

Please, add another space between the last “a” and the ". this will make the code show up as

“I want to eat a mango” etc etc.


#3

Just made those changes thanks! But still in the JavaScript lesson it says that .forEach ALWAYS returns “undefined” and I don’t know what that means.


#4

Is it the SCT that is giving your that message, or is it a console error? I’m not able to reproduce this issue.


.forEach iteration method is a kind of undefined return value?
#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.