How to use forEach loop over Arrays in Javascript?


+ 1

I'm stuck on JavaScript basics, forEach looping over arrays. Can anyone explain me?

1 Answer

5 years ago by

+ 2

forEach method is used to run a function on every element present in an array. This method can only be used on Arrays, Maps and Sets.

You can either use callback functions or Arrow functions in forEach method.

Remember that you can not break, return or continue in forEach method like you do in traditional for loop. If you want to do so, then use exceptions in the call back function.

forEach Syntax


Example1 : Using Callback function


click here for results.

Example2 : Using Arrow function


click here for results.

5 years ago by Divya



Your Answer