Why difference in result for same .forEach() function?

const fruits = ['mango', 'papaya'];

let forEach = fruits.forEach(fruit => fruit.length > 6) 

let forEach2 = fruits.forEach(fruit => console.log('Hi ' + fruit)) / result is "Hi mango" and "Hi papaya"

console.log('For Each Result is: ' + forEach) // result is "undefined"

console.log('For Each2 Result is: ' + forEach2) // result is "undefined"

Why the result in line 3 is different from line 5?

Hey there natlam5291539322!
I think the problem you’re encountering is due to the fact that the .forEach() method executes something for each element of an array, but doesn’t actually return anything, so when you console.log the variables forEach and forEach2 you get undefined.

Why the result in line 3 is different from line 5?

When you console.log inside the forEach2 at line 3, you’re executing an action on the single array elements of fruits, and so it works for each one of them :wink:

Interesting the possibility of naming a variable forEach, as I thought was a reserved name. I would avoid doing that however, can be messy.

Check out this MDN article about the .forEach() method if you want to know more.

Hope this helps. :slight_smile: