Skip to content

Commit d9ee6e3

Browse files
authored
Reordering sections
`forEach` is mentioned as known in **reduce/reduceRight** which is a subsection of **Transform an array**, and this section is above **Iterate: forEach** where we first learn about `forEach`.
1 parent 4e5aef8 commit d9ee6e3

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

1-js/05-data-types/05-array-methods/article.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,35 @@ let arrayLike = {
201201
alert( arr.concat(arrayLike) ); // 1,2,something,else
202202
```
203203

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+
204233
## Searching in array
205234

206235
These are methods to search for something in an array.
@@ -584,35 +613,6 @@ So it's advised to always specify the initial value.
584613

585614
The method [arr.reduceRight](mdn:js/Array/reduceRight) does the same, but goes from right to left.
586615

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-
616616
## Array.isArray
617617

618618
Arrays do not form a separate language type. They are based on objects.

0 commit comments

Comments
 (0)