Thursday, January 16, 2025
HomeProgrammingjavascript fold reduce functional programming

javascript fold reduce functional programming

In JavaScript, reduce() is a higher-order function used for accumulating or “folding” values in an array, similar to the fold operation in functional programming. It applies a callback function to each element, accumulating a result. The syntax is:

javascript
array.reduce((accumulator, currentValue) => {
// Logic to combine accumulator and currentValue
return newAccumulator;
}, initialValue);
  • accumulator: The accumulated value across iterations.
  • currentValue: The current element being processed.
  • initialValue: The initial value for the accumulator.
See also  Genetic Algorithm in Machine Learning

Example: Summing numbers in an array:

javascript
const sum = [1, 2, 3].reduce((acc, val) => acc + val, 0); // 6

This is an essential tool for functional programming in JavaScript.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x