From the functional programming point of view:
forEach is a consumer operation and hence the terminal operation in the stream so you can only consume the input but cannot generate the output and pass it to next stream. Programatically, your forEach code block should not contain return statement.
ex:
arr.forEach( val => {
//... logic to use val
// but no return statement
});
map is a transform operation in the stream i.e., you take X as the input and produce Y as the output and optionally pass it to the next stream. Programatically, code block inside map operation must return some value.
ex (purposely written with brevity for syntactical understanding) :
const numArr = [1, 2, 3, 4, 5];
// first example
const numDoubledArr = numArr.map( num => {
return num + num; // transformation
});
// second example
numArr
.map( num => {
return num + num; // transformation
})
.forEach( doubledNum => {
console.log('Doubled Number: ', doubledNum);
});