Open In App

Node forEach() function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

forEach() is an array function Node that is used to iterate over items in a given array.

Syntax:

array_name.forEach(function)

Parameter:

This function takes a function (which is to be executed) as a parameter.

Return type:

The function returns an array element after iteration. The program below demonstrates the working of the function:

Example 1: Below is the code example of forEach() function

Javascript




const arr = ['cat', 'dog', 'fish'];
arr.forEach((element) => {
    console.log(element);
});


Output:

cat
dog
fish

Example 2: Below is the code example of forEach() function

Javascript




const arr = [1, 2, 3, 8, 7];
arr.forEach((element) => {
    console.log(element);
});


Output:

1
2
3
8
7

Last Updated : 15 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads