loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
// Syntax: array.reduce(callback[, initialValue])

function sum(previousResult, currentElement, currentIndex, array) {
  console.log("previousResult (accumulator):", previousResult);
  console.log("currentElement (current element value):", currentElement);
  console.log("currentIndex (current element array index):", currentIndex);
  console.log("array (the array):", array);
  console.log("--------------------------------");
  return previousResult + currentElement;
}
console.log("result (5+10+15):", [5, 10, 15].reduce(sum));
console.log("________________________________");
console.log("result (100+5+10+15):", [5, 10, 15].reduce(sum, 100));
Native Browser JavaScript