Search posts, tags, users, and pages
JavaScript Map, Reduce and Filter Functions Introduction Arrays are important Data Structures in programming. All the methods which we are going to discuss in this article will iterate over an array and return a new array based on the result function...
Subha Chanda
@nemo
Technical Writer | Developer
Markdown supported · ⌘↵ to post
Michael Geary
const sum = arr.reduce((result, item) => { result = result + item; return result; });
Since result is local to the callback, you can simplify this to:
result
const sum = arr.reduce((result, item) => { return result + item; });
Thank you for the suggestion. 😊 I was thinking to use this code, const sum = arr.reduce((result, item) => result + item); But because it's not much beginner-friendly, so I chose to go with the one above.
Bolaji Ayodeji
Really insightful, thanks for sharing.
Michael Geary
const sum = arr.reduce((result, item) => { result = result + item; return result; });Since
resultis local to the callback, you can simplify this to:const sum = arr.reduce((result, item) => { return result + item; });