---
title: "Comment by samyek sonawane on \"Functional JavaScript: How to use array reduce for more than just numbers\" | Hashnode"
description: "Great Work James Sinclair Loved your Topic, even i am interested in .reduce() The first Example will be hard for the starters to understand, So here&#39;s my Ve"
url: "https://hashnode.com/posts/functional-javascript-how-to-use-array-reduce-for-more-than-just-numbers/5ce546b532b49d9b601220e7/comment/5ce8414b98f3abc4058bf1ec"
---

[Discussion](https://hashnode.com/posts/functional-javascript-how-to-use-array-reduce-for-more-than-just-numbers/5ce546b532b49d9b601220e7)

Comment [Functional JavaScript: How to use array reduce for more than just numbers](https://hashnode.com/posts/functional-javascript-how-to-use-array-reduce-for-more-than-just-numbers/5ce546b532b49d9b601220e7)

[samyek sonawane](https://hashnode.com/@IcedMonk)

[May 24, 2019](https://hashnode.com/posts/functional-javascript-how-to-use-array-reduce-for-more-than-just-numbers/5ce546b532b49d9b601220e7/comment/5ce8414b98f3abc4058bf1ec)

# Great Work [James Sinclair](https://hashnode.com/@jrsinclair)

> Loved your Topic, even i am interested in .reduce()

**The first Example will be hard for the starters to understand, So here's my Version!**

## .reduce()

-   Accepts a callback function and an optional second parameter
-   Iterates through an array
-   Runs a callback on each value in the array
-   The first parameter to the callback is either the first value in the array or the optional second parameter
-   The first parameter to the callback is often called "accumulator"
-   The returned value from the callback becomes the new value of accumulator \_ Whatever is returned from the callback function, becomes the new value of the accumulator!

### 1\. Example without adding the second parameter

```
var arr = [1,2,3,4,5];

arr.reduce(function(accumulator, nextValue){
    return accumulator + nextValue;
});
```

**This will generate the following result**

```
   accumulator    nextValue   returnedValue
    1              2             3
    3              3             6
    6              4             10
    10             5             15
```

**_And Hence the result will be_ 15**

### 2\. Example with adding the second parameter

```
var arr = [1,2,3,4,5];

arr.reduce(function(accumulator, nextValue){
    return accumulator + nextValue;
},10);
```

**_It will generate the following_. Keep in mind, if the second parameter is passed then this is treated as the first value in the array**

```
accumulator    nextValue   returnedValue
    10             1             11
    11             2             13
    13             3             16
    16             4             20
    20             5             25
```

# **There are N number of possibilities to play with .reduce()**

**I hope you find my answer helpful, please do like and comment it gives me the confidence to be more helpful and innovative😇**
