30秒学会 JavaScript 片段 · 2023年1月21日

30秒学会 JavaScript 片段 – What is the difference between Array.prototype.map() and Array.prototype.forEach()?

Array.prototype.map() and Array.prototype.forEach() are two of the most commonly used methods in JavaScript. Both of them iterate over an array and perform some action on each element. Yet, they’re not the same and they’re not interchangeable.

The key difference between the two lies in the return value. The return value of Array.prototype.map() is a new array with the results of the callback function. On the other hand, the return value of Array.prototype.forEach() is undefined.

Simply put, Array.prototype.forEach() is used to perform some action on each element in an array, while Array.prototype.map() is used to create a new array based on the elements in the original array. Let’s take a look at an example to clear up any confusion:

代码实现

const numbers = [1, 2, 3, 4, 5];

numbers.forEach(num => console.log(num * 2));
// No return value, output: 2, 4, 6, 8, 10

const doubledNumbers = numbers.map(num => num * 2);
// Returns a new array: [2, 4, 6, 8, 10]

The way I like to distinguish them is by remembering that Array.prototype.map() represents a transformation, whereas Array.prototype.forEach() represents an iteration. Hopefully, one of these explanations will click for you and help you remember the difference between the two.

As a closing note, I would like to remind you that the humble for loop can be more efficient in some cases, such as breaking out of a loop early. Always pick the right tool for the job, as ES6 has a method for almost every use case.

翻译自:https://www.30secondsofcode.org/js/s/array-map-vs-foreach