“*** .forEach()
is used to execute the same code on every element in an array but does not change the array and returns undefined
.**”
What does it mean "returns ‘undefined’ " here?
when run the code with .forEach method, it works well and returns the expected content, so it doesn’t look undefined to me.
if there is no return keyword in a function or method, undefined is implicitly returned.
look:
var array1 = ['a', 'b', 'c'];
result = array1.forEach(function(element) {
console.log(element);
});
console.log(result);
as you can see, we capture the “returned” result of the method in a variable. If we then log this variable, we see undefined.
so yea, the forEach function logs all the different elements, but the method itself returns undefined
1 Like
I had the same confusion, but this explains.
Thanks.