map and forEach, why the third one is invalid
let arr0=[1,2,3,5,0,9,1000,294,85,3850];
console.log(arr0.map(x=>x+x));
arr0.map(x=>console.log(x+x));
console.log(arr0.forEach(x=>x+x)); // ??
arr0.forEach(x=>console.log(x+x));
How to output the value of forEach as an array??
Okay, actually x=>x+x is an abbreviation, which is equivalent to x=>{return x+x;}; return in forEach will terminate the traversal
forEach
的返回值是undefined
forEach has no return value and needs to be written in two steps, or use the last method
If you want this effect, you need to manually create a new array and operate it in forEach
As the name suggests, for each means "for each operation", and map means "one-to-one matching".
So
forEach
will not care about the return value.It’s pointless to ask why something like this is artificially set, check the API more.