- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Node.js β forEach() Method
The forEach() method in Node.js is used for iterating over a set of given array items. One can iterate over all the values of the array one-by-one using the forEach array loop.
Syntax
arrayName.forEach(function)
Parameters
- function − The function takes input for the method that will be executed.
- arrayName − Array that will be iterated.
Example 1
Create a file "forEach.js" and copy the following code snippet. After creating the file, use the command "node forEach.js" to run this code.
// forEach() Demo Example // Defining a vehicle array const vehicleArray = ['bike', 'car', 'bus']; // Iterating over the array and printing vehicleArray.forEach(element => { console.log(element); });
Output
C:\home
ode>> node forEach.js bike car bus
Example 2
// forEach() Demo Example // Defining a vehicle array const array = [1,2,3,4,5,6,7,8,9]; var sum=0; // Iterating over the array and printing array.forEach(element => { sum += element; }); console.log(sum);
Output
C:\home
ode>> node forEach.js 45
Advertisements