JavaScriptBeginner#arrays

Difference between forEach and map?

forEach runs a callback for side effects and returns undefined, so it cannot be chained. map returns a new array of results and should not be used purely for side effects.

Example
const out = [1,2].forEach(x => x*2); // undefined
const arr = [1,2].map(x => x*2);     // [2,4]

Related Questions

1
JavaScriptBeginner#arrays

Difference between slice and splice?

Open
2
JavaScriptBeginner#arrays

What do find, findIndex, some and every do?

Open
3
JavaScriptBeginner#es6

What is destructuring?

Open