---
title: "Discussion on \"Functional JavaScript: How to use array reduce for more than just numbers\" | Hashnode"
description: "Discussion on \"Functional JavaScript: How to use array reduce for more than just numbers\". Reduce is the Swiss-army knife of array iterators. It’s really powerful. So powerful, you can build most of the other array iterator methods with it, like .map(), .filter() and .flatMap(). And in this article we’ll look at some more amazing things yo..."
url: "https://hashnode.com/posts/functional-javascript-how-to-use-array-reduce-for-more-than-just-numbers/5ce546b532b49d9b601220e7"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Functional JavaScript: How to use array reduce for more than just numbers](https://hashnode.com/post/functional-javascript-how-to-use-array-reduce-for-more-than-just-numbers?utm_source=hashnode&utm_medium=feed)

Reduce is the Swiss-army knife of array iterators. It’s really powerful. So powerful, you can build most of the other array iterator methods with it, like .map(), .filter() and .flatMap(). And in this article we’ll look at some more amazing things yo...

May 22, 2019 · hashnode.com  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#functional-programming](https://hashnode.com/tag/functional-programming)

## Written by

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

@jrsinclair · Australia

  
[View profile](https://hashnode.com/@jrsinclair)

Web Developer

## Responses 3

Markdown supported · ⌘↵ to post

[Joseph S Stevens](https://hashnode.com/@josephscottstevens)

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

Good read!

[Tommy Hodgins](https://hashnode.com/@tomhodgins)

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

The most common thing I use `reduce()` for is producing Strings as output, by looping over and processing a list of some kind of data. I might be doing something like:

-   reducing an `Array` of data into a String of HTML source
-   reducing a `NodeList` of elements (like what gets returned from `document.querySelectorAll()`) in a webpage into a String of CSS source
-   reducing a `CSSStyleSheet` into a String of CSS source
-   reducing an `Array` of data into other formats (CSV, MD, TXT, JSON, JS, etc) and sometimes also outtputting that as data URIs

I think the only time I've used it on numbers has been as a demo summing an `Array` of numbers to show what it can do, not for anything I've actually needed yet :D

[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😇**
