DEV Community

KevenParisien
KevenParisien

Posted on

Array.prototype.forEach()

Information on this post has been referenced from:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

forEach() Description:

The forEach() method calls a defined "callbackFn" one time for each element inside of an array. The "callbackFn" is invoked using three arguements:

1.

The value of the element

2.

The index of the element

3.

The Array object being traversed

Syntax:

  1. forEach((element)
  2. forEach((element, index)
  3. forEach((element, index, array)

Example:

Image description

Output:

In the example provided above, the "array1.forEach" line is going into array1 and console.log(ging) each element inside of the Array("a", "b", "c"). If the elements were ordered ("c", "b", "a") then it would log the elements in that order.

More Information In Link Provided At The Top Of This Post

Discussion (0)