JavaScript: Iterators - .forEach Method page 2/7


#1

“Learn:” part says there are three important things to know about .forEach method.

  1. it is an array method. It must be called upon an array.

  2. Any changes to the iterated array value won’t be updated in the original array.

  3. The return value is undefined

I don’t get the number 3 that says “The return value is undefined” , because the code I made didn’t return any “Undefined” result at the consoles.

The instruction is:

  1. Iterate over the fruits array to log I want to eat a plus the name of each fruit to the console. For example, I want to eat a mango.
    Use function(parameter) syntax for this step (not arrow function => syntax).

  2. Refactor the .forEach() call you just wrote using arrow function syntax instead.

My code is:
let fruits = [‘mango’, ‘papaya’, ‘pineapple’, ‘apple’];

// Iterate over fruits below
fruits.forEach(function(fruitsItem){
console.log(‘I want to eat a ’ + fruitsItem);
});
console.log(’’);
//Refactored using Arrow function syntax.
fruits.forEach(fruitsItem => console.log('I want to eat a ’ + fruitsItem));

console:
I want to eat a mango
I want to eat a papaya
I want to eat a pineapple
I want to eat a apple

I want to eat a mango
I want to eat a papaya
I want to eat a pineapple
I want to eat a apple

Syllabus Number 7 Iterators of JavaScript, Page 2/7.

Again, What I don’t understand is the number 3 of “Things to know about forEach method.” Which says it returns undefined but I didn’t get any Undefined result from the console.


#2

Would it have? Make sure to keep printing and returning apart, different things.

Does it return the number 5? If not, how are you able to tell, and shouldn’t you then also be able to tell what it does return?