foreach on array cannot see function code example

Example 1: foreach in javascript

///Simple One
var a = ["a", "b", "c"];
a.forEach(function(entry) {
    console.log(entry);
});


///Function  concept

var fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);

function myFunction(item, index) {
  document.getElementById("demo").innerHTML += index + ":" + item + "<br>";
}

Example 2: what is foreach method in javascript

var arr = [1, 2, 3, 4, 5, 6];

//var sum = 0;
arr.forEach(function (value, index, array){  // Built in method
    //sum += value;
    console.log(value, index, array);
})

//console.log(sum);  // We can print sum of all Array element (if those are numbers)

// function forEach(arr, cb){  // How forEach() method works
//     for (var i = 0; i<arr.length; i++){
//         cb(arr[i], i, arr);
//     }
// }
// var sum = 0;
// forEach(arr, function (value, index, arr){
//     console.log(value, index, arr);
//     sum += value;
// })
// console.log(sum);