@@ -201,6 +201,35 @@ let arrayLike = {
201
201
alert ( arr .concat (arrayLike) ); // 1,2,something,else
202
202
```
203
203
204
+ ## Iterate: forEach
205
+
206
+ The [ arr.forEach] ( mdn:js/Array/forEach ) method allows to run a function for every element of the array.
207
+
208
+ The syntax:
209
+ ``` js
210
+ arr .forEach (function (item , index , array ) {
211
+ // ... do something with item
212
+ });
213
+ ```
214
+
215
+ For instance, this shows each element of the array:
216
+
217
+ ``` js run
218
+ // for each element call alert
219
+ [" Bilbo" , " Gandalf" , " Nazgul" ].forEach (alert);
220
+ ```
221
+
222
+ And this code is more elaborate about their positions in the target array:
223
+
224
+ ``` js run
225
+ [" Bilbo" , " Gandalf" , " Nazgul" ].forEach ((item , index , array ) => {
226
+ alert (` ${ item} is at index ${ index} in ${ array} ` );
227
+ });
228
+ ```
229
+
230
+ The result of the function (if it returns any) is thrown away and ignored.
231
+
232
+
204
233
## Searching in array
205
234
206
235
These are methods to search for something in an array.
@@ -584,35 +613,6 @@ So it's advised to always specify the initial value.
584
613
585
614
The method [ arr.reduceRight] ( mdn:js/Array/reduceRight ) does the same, but goes from right to left.
586
615
587
-
588
- ## Iterate: forEach
589
-
590
- The [ arr.forEach] ( mdn:js/Array/forEach ) method allows to run a function for every element of the array.
591
-
592
- The syntax:
593
- ``` js
594
- arr .forEach (function (item , index , array ) {
595
- // ... do something with item
596
- });
597
- ```
598
-
599
- For instance, this shows each element of the array:
600
-
601
- ``` js run
602
- // for each element call alert
603
- [" Bilbo" , " Gandalf" , " Nazgul" ].forEach (alert);
604
- ```
605
-
606
- And this code is more elaborate about their positions in the target array:
607
-
608
- ``` js run
609
- [" Bilbo" , " Gandalf" , " Nazgul" ].forEach ((item , index , array ) => {
610
- alert (` ${ item} is at index ${ index} in ${ array} ` );
611
- });
612
- ```
613
-
614
- The result of the function (if it returns any) is thrown away and ignored.
615
-
616
616
## Array.isArray
617
617
618
618
Arrays do not form a separate language type. They are based on objects.
0 commit comments