Node.js forEach

Node.js forEach is used to execute the provided function for each element.

Syntax – forEach

The syntax of forEach is;

myFunction function is executed for each element in arr. The element of array is passed as argument to the function during each iteration.

Example 1: forEach on Array of elements

In this example, we will use forEach to apply on each element of array.

Output

Node.js forEach

Example 2: forEach on Array of elements with external function passed as argument

In this example, we will use forEach to apply on each element of array. And we define the function separately and pass as argument to forEach.

Example 3: forEach on Array with access to Element, Index and Array

In this example, we will access index and array along with element in each iteration.

Output

Node.js forEach - Access element, index, array