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

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

Syntax

array.forEach(function(currentValue, index, arr), thisValue)

Parameters

function() Required.
A function to run for each array element.
currentValue Required.
The value of the current element.
index Optional.
The index of the current element.
arr Optional.
The array of the current element.
thisValue Optional. Default undefined.
A value passed to the function as its this value.

Return Value

undefined
<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var a = ["Rahul","Karan","Aman","Neha"];
	a.forEach(function(value,index) {
		document.write(index + " : " + value + "<br>");  
	})
  </script>
</head>
<body>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript</title>
  <script>
    var a = ["Rahul","Karan","Aman","Neha"];
    a.forEach(loop);
    function loop(value, index){
      document.write(index + " : " + value + "<br>");
    }
  </script>
</head>
<body>
</body>
</html>