Node js - Foreach Loop - Node JS tutorial



For Loop

  • It is an advanced looping structure which is used in traversing items in a collection. It is executed until the collection set is over.
  • Difference from for loop is that it works with variables but foreach works with the object.
  • Foreach loops usually doesn’t have any explicit counter.
 nodejs foreach loop

Learn Nodejs - Nodejs tutorial - nodejs foreach loop - Nodejs examples - Nodejs programs

Sample code :

a=[1,2,3];
a.forEach((v) => {
console.log("The value of a is :"+v);
});

Read Also

For Loop in C

Code Explanation :

 nodejs foreach loop code

Learn Nodejs - Nodejs tutorial - nodejs foreach loop code - Nodejs examples - Nodejs programs

  • Array object is declared as a=[1,2,3];
  • Foreach loop on a is written such that foreach value v from the object a, the value is written to the console. The following code given below.
a.forEach((v) => {
console.log("The value of a is :"+v);
});

Output :

 nodejs foreach loop code output

Learn Nodejs - Nodejs tutorial - nodejs foreach loop code output - Nodejs examples - Nodejs programs

Read Also

Java For Loop

Related Searches to node.js - Foreach Loop - Node.js tutorial