JavaScript forEach Method or Loop


 Want to understand Properly Plz see the video carefully on YouTube channel name is Adisma


The forEach() method calls a function for each element in an array.

The forEach() method is not executed for empty elements.


 <!DOCTYPE html>

<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1>JavaScript forEach Loop / Method</h1>
  <script>
    var myNumbers = [2,5,8,10,30];
 let sum = 0;
    myNumbers.forEach(function(curVal, indexOfElement, curArr){

      console.log("Value = " + curVal , 'IndexNo= ' +  indexOfElement , "of array =" + curArr);

      sum = sum + curVal;
    })
    console.log("Sum of array Element = " + sum);
   

  </script>
</body>

</html>

Comments

Popular posts from this blog

Javascript Array Concat and Join Method

JavaScript While Loop

JavaScript pop() Method