Skip to content

Instantly share code, notes, and snippets.

@waimyokyaw
Created March 17, 2018 08:23
Show Gist options
  • Save waimyokyaw/07e23dd3df204df28d6999fd78937f8a to your computer and use it in GitHub Desktop.
Save waimyokyaw/07e23dd3df204df28d6999fd78937f8a to your computer and use it in GitHub Desktop.
Sum elements of array using javascript
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment