---
title: "Recursive Map, Reduce and Filter in JavaScript"
description: "Functional implementations of map, reduce, and filter using recursion. Practical notes and code examples on Recursive Map."
url: "https://h3manth.com/scribe/recursive-map-reduce-and-filter-in-javascript/"
canonical_url: "https://h3manth.com/scribe/recursive-map-reduce-and-filter-in-javascript/"
markdown_url: "https://h3manth.com/scribe/blog/recursive-map-reduce-and-filter-in-javascript.md"
publishDate: "2015-09-06T00:00:00.000Z"
author: "Hemanth HM"
category: "javascript"
tags: ["javascript","functional"]
word_count: 108
reading_time_minutes: 1
x_markdown_tokens: 253
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Recursive Map, Reduce and Filter in JavaScript

> Functional implementations of map, reduce, and filter using recursion. Practical notes and code examples on Recursive Map.

A more functional way to implement map, reduce, and filter:

## Recursive Map

```javascript
let map = (fn, list) => 
  !list.length ? [] : [fn(list[0])].concat(map(fn, list.slice(1)));

map(x => x + 1, [1, 2, 3, 4]); // [2, 3, 4, 5]

## Recursive Reduce
javascript
let reduce = (fn, value, list) => 
  !list.length ? value : reduce(fn, fn(value, list[0]), list.slice(1));

reduce((x, y) => x + y, 0, [1, 2, 3, 4]); // 10

## Recursive Filter
javascript
let filter = (predicate, list) => 
  !list.length ? [] : 
    (predicate(list[0]) ? [list[0]] : [])
      .concat(filter(predicate, list.slice(1)));

filter(x => x > 2, [1, 2, 3, 4, 5]); // [3, 4, 5]
```
