Take the 2-minute tour ×

I use the identity function in all my JavaScript programs:

function identity(value) {
    return value;

The reason is that I often need differentiate between primitives types (undefined, null, boolean, number and string) and object types (object and function) as returned by the typeof operator. I feel using the indentity function for this use case very succuint:

if (new identity(value) == value); // value is of an object type
if (new identity(value) != value); // value is of a primitive type

The identity function is much smaller and simpler than the following code:

function isObject(value) {
    var type = typeof value;
    return type == "object" || type == "function";

However on reading my code a friend of mine complained that my hack is misleading and more computationally expensive than the above alternative.

I don't want to remove this function from any of my programs as I believe it's an elegant hack. Then again I don't write programs solely for myself. Is there any other use case for the identity function in JavaScript?

share|improve this question
Why not just use value === Object(value)? Or, for more readable code, define isObject to use it? –  Cory May 2 '13 at 21:45
@Cory - I just wanted to find a practical use case for the identity function. I had too much time on my hands and so I worded this question interestingly to catch the attention of people and to not come across as a stupid idiot asking for a use case for the identity function. –  Aadit M Shah May 4 '13 at 12:31
One of the best uses is making code that may or may not apply a function work consistently: var func = optionalProcessFunc || identityFunc;. Now later on in the code, you don't need to know anything about how this section was configured to run. More generally, if you want to treat functions as monoids, it's convenient to use the identity function as mzero and compose as mconcat. –  Cory May 6 '13 at 11:31

2 Answers 2

up vote 3 down vote accepted

I updated my "speedtest" to test if the right results are returned … they aren't:

If you compare with new identity(x) == x, then null is deemed an object. === works, though.

Such pitfalls speak in favor of the isObject(...) solution.

If you compare === 'object'/'function' in the isObject code, then it will be double as fast as your original implementation, and almost a third faster than new identity(x) === x.

share|improve this answer


new identity(value) == value

means absolutely nothing and without extra comment I would have to think for a while to figure out what the intent was. On the other hand:


is obvious from the very beginning, no matter how it is implemented. Why can't you use your hack inside a function named isObject()?

BTW More suited for http://codereview.stackexchange.com.

share|improve this answer
new identity(value) is a nice little hack. JavaScript engines only return the instance of a constructor if the constructor does not explicitly return an object (i.e. a value of the type object or function). Hence new identity(value) is always an object. Thus new identity(value) == value only returns true if value is an object. This is because the equality operators in JavaScript always check for identity when one of the operands is an object. Nice pun right? –  Aadit M Shah Jul 14 '12 at 16:56
@AaditMShah: I am really impressed (BTW using === might prove little faster) and I'm sure it works. What I mean is: no matter how fast it is, many people, including myself, will have no idea what it does. And (surprise!) you write software for people, not computers. –  Tomasz Nurkiewicz Jul 14 '12 at 17:02
Yes. That is what my friend said too. Still when you are have some time off and have nothing better to do it's fun to see how you can use existing technology in weird and innovative ways. –  Aadit M Shah Jul 14 '12 at 17:07
BTW you are correct. Using strict equality does improve performance. Thank you. –  Aadit M Shah Jul 14 '12 at 17:10

Your Answer


