Skip to content

Instantly share code, notes, and snippets.

@swashata
Created November 30, 2018 13:23
Show Gist options
  • Save swashata/659e9f9e22ea2fb066ada17a7b385938 to your computer and use it in GitHub Desktop.
Save swashata/659e9f9e22ea2fb066ada17a7b385938 to your computer and use it in GitHub Desktop.
Array Reduce
const arr = [1, 2, 3, 4, 5];
// Without initial value
const sumOfArr = arr.reduce((acc, cur) => {
return acc + cur;
});
console.log(sumOfArr);
// With initial value
const sumWithInitialValue = arr.reduce((acc, cur) => {
return acc + cur;
}, 999); // 999 passed as second parameter of arr.reduce function
console.log(sumWithInitialValue);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment