What is foreach in javascript?

 array foreach method javascript:

 

forEach() method need to callback function and function be execute only once time for the an array each value. for each not working without any array value.

Syntax of forEach method:

array.forEach(function)

 

foreach example in javascript:

 

const colors = ["Red", "blue", "Black", "Orange", "White"];

 

colors.forEach(e => console.log(e));

 

// "Red"

// "blue"

// "Black"

// "Orange"

// "White"

 

// Another example...

var sum = 0;

var numbers = [10, 20, 30, 40, 50];

numbers.forEach(func);

 

function func(item) {

  sum += item;

  console.log(sum);

}

 

// "10"

// "30"

// "60"

// "100"

// "150"




No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.