---
title: "Practical usage of identity function"
description: "Practical usage of identity function"
canonical_url: "https://www.bigbinary.com/blog/practical-usage-of-identity-function"
markdown_url: "https://www.bigbinary.com/blog/practical-usage-of-identity-function.md"
---

# Practical usage of identity function

Practical usage of identity function

- Author: Rohit Kumar
- Published: March 20, 2018
- Categories: Misc

If you are learning functional programming then you can't go far
[without running into](https://gist.github.com/Avaq/1f0636ec5c8d6aed2e45)
"identity function".

An identity function is a very basic function that

- takes one argument
- returns the argument

```javascript
f(x) = x;
```

This seems like the most useless function in the world. We never needed any
function like this while building any application. Then what's the big deal
about this identity function.

In this blog we will see how this identity concept is used in the real world.

For the implementation we will be using [Ramda.js](http://ramdajs.com/). We
previously
[wrote about](https://blog.bigbinary.com/2017/10/06/optimize-javascript-code-for-composability-with-ramdajs.html)
how we, at BigBinary, write JavaScript code using Ramda.js.

Again please note that in the following code `R` stands for `Ramda` and not for
[programming language R](https://www.r-project.org).

### Example 1

Here is JavaScript code.

```javascript
if (x) return x;
return [];
```

Here is same code using Ramda.js.

```javascript
R.ifElse(R.isNil, () => [], R.identity);
```

[try it](http://ramdajs.com/repl/?v=0.25.0#?const%20fn%20%3D%20R.ifElse%28%0A%20%20R.isNil%2C%0A%20%20%28%29%20%3D%3E%20%5B%5D%2C%0A%20%20R.identity%0A%20%29%3B%0A%0Afn%28null%29%3B%0Afn%28%22hello%22%29%3B)

### Example 2

Here we will use identity as the return value in the default case.

```javascript
R.cond([
  [R.equals(0), R.always("0")],
  [R.equals(10), R.always("10")],
  [R.T, R.identity],
]);
```

[try it](http://ramdajs.com/repl/?v=0.25.0#?const%20fn%20%3D%20R.cond%28%5B%0A%20%20%5BR.equals%280%29%2C%20R.always%28%220%22%29%5D%2C%0A%20%20%5BR.equals%2810%29%2C%20R.always%28%2210%22%29%5D%2C%0A%20%20%5BR.T%2C%20R.identity%5D%0A%5D%29%3B%0A%0A%0Afn%280%29%3B%0Afn%2810%29%3B%0Afn%285%29%3B)

### Example 3

Get the unique items from the list.

```javascript
R.uniqBy(R.identity, [1, 1, 2]);
```

[try it](http://ramdajs.com/repl/?v=0.25.0#?R.uniqBy%28R.identity%2C%20%5B1%2C1%2C2%5D%29)

### Example 4

Count occurrences of items in the list.

```javascript
R.countBy(R.identity, ["a", "a", "b", "c", "c", "c"]);
```

[try it](http://ramdajs.com/repl/?v=0.25.0#?R.countBy%28R.identity%2C%20%5B%22a%22%2C%22a%22%2C%22b%22%2C%22c%22%2C%22c%22%2C%22c%22%5D%29%3B)

### Example 5

Begin value from zero all the way to n-1.

```javascript
R.times(R.identity, 5);
```

[try it](http://ramdajs.com/repl/?v=0.25.0#?R.times%28R.identity%2C%205%29)

### Example 6

Filter truthy values.

```javascript
R.filter(R.identity, [
  { a: 1 },
  false,
  { b: 2 },
  true,
  "",
  undefined,
  null,
  0,
  {},
  1,
]);
```

[try it](http://ramdajs.com/repl/#?R.filter%28R.identity%2C%20%5B%7Ba%3A1%7D%2C%20false%2C%20%7Bb%3A2%7D%2C%20true%2C%20%27%27%2C%20undefined%2C%20null%2C%200%2C%20%7B%7D%2C%201%5D%29%3B)

## Links

- [Human page](https://www.bigbinary.com/blog/practical-usage-of-identity-function)
