# protypeMethodのmap()とforEachの違い

## Array.prototype.map() とは

`map()`メゾットは、与えられた関数を配列のすべての要素に対して呼び出し、<br>
その結果からなる**新しい配列を生成**する。
```javascript:example.js
const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

return console.log(map1);
```

結果
```javascript:example.js
> Array [2, 8, 18, 32]
```

## Array.prototype.forEach() とは

`forEach() `メソッドは与えられた関数を、配列の各要素に対して**一度ずつ実行**する。
```javascript:example.js
const array1 = ['a', 'b', 'c'];

return array1.forEach(x => console.log(x));
```

結果
```javascript:example.js
> "a"
> "b"
> "c"
```

## つまり、map()とforEach()の違いは?
- map()はmap()内の作業を各要素ごとに実行し新しい配列を生成すること。
- forEach()はforEach()内の=>以降の作業を要素分、一行ずつ実行される。

---

## Page Navigation

- Canonical URL: https://tips.weseek.co.jp/用語集/Javascript/protypeMethodのmap()とforEach()の違い
- Permalink: https://tips.weseek.co.jp/5e57b3b07887d100480949fe
- Parent: [Javascript](/5e392e88e848c600487d69db.md)
- Children: 0 total
- Total descendants: 0
- Siblings: 14 total
  - [Angular](/5e393953e848c600487d6a04.md)
  - [ECMAScript](/5e39820de848c600487d6a24.md)
  - [Object_entries ](/5e3f9391f5921400485be6a8.md)
  - [React](/5e3930c8e848c600487d69ed.md)
  - [Setオブジェクト](/5e4252bbdecd0a0049953aea.md)
  - [Svelte](/5e3980bde848c600487d6a1e.md)
  - [Vue](/5e3930f3e848c600487d69f0.md)
  - [protypeMethodのthen()について](/5f163aac647aac00495efa7d.md)
  - [throttle-debouceのthrottleとdebouceの違い](/5e785068f78b4b0048b6add1.md)
  - [スプレッド構文](/5e39828be848c600487d6a27.md)
  - [ローカルストレージ](/5e86b4db139099004805819c.md)
  - [分割代入](/5e3af26c25751e0049929801.md)
  - [宣言（varとletとconstの違い）](/5e5de1da3051ce00499cac5e.md)
  - [正規表現](/5e633bb4d9ebf700489783ea.md)
- Last updated: 2020-02-27T12:56:58.973Z by deleted_at_1672138394172
- Full page listing (all children regardless of count): https://tips.weseek.co.jp/_api/v3/page-listing/children?id=5e57b3b07887d100480949fe
