# forEach * forEach() 方法會將陣列內的每個元素,皆傳入並執行給指定的函式一次。 * 可用來取代 for 的寫法,讓程式碼更簡短,省略掉定義 i、arr.length…等動作。 ### 語法 > array.forEach(function(currentValue, index, arr), thisValue) | 參數 | 描述 | | -------- | -------- | | currentValue | 常用參數 / 當前元素 | | index | 選用參數 / 當前元素索引值 | | arr | 選用參數 / 當前元素的數組對象 | > forEach() 沒有第二參數thisArg 的範例 > ![](https://i.imgur.com/YEWyVZR.png) ###### 沒有第二參數 thisArg 的結果顯示為 : 6 # > forEach() 有第二參數thisArg 的範例 > 第二個參數 thisArg 是可選擇性的,如果有這個參數,它就會被作為 callback 回呼函式,每次被調用的 this 的值。 ![](https://i.imgur.com/R4Y9GNG.png) ###### 有第二參數 thisArg 的結果顯示如下 : ![](https://i.imgur.com/NHjkcnn.png)