What exactly does the anonymous JavaScript function f => f do?

SomethingOn picture SomethingOn · Dec 20, 2016 · Viewed 25.3k times · Source

I'm using a third-party library that has a function that takes functions as arguments. I'm doing some conditional checks to decide whether or not to add a particular function as a parameter and in some cases I don't want to provide a function. Providing null in that cases throws an error.

I found this code which works, but I don't fully understand what's happening.

compose(__DEV__ ? devTools() : f => f)

Is f => f equivalent to () => {} an empty anonymous function?

Answer

Felix Kling picture Felix Kling · Dec 20, 2016

f => f is the identity function. It simply returns the argument that was passed in.

This function is often used as a default values for transformation processes, since it doesn't perform any transformation.

Is f => f equivalent to () => {} an empty anonymous function?

No. The empty function doesn't return anything. The identity function returns the passed in argument.