Skip to content

What is replacing .then(data => console.log(data)) with just .then(console.log) called?

I saw the following code in a React application.

getData()
        .then(res => res.json())
        .then(console.log)

And it behaves exactly the same as I would use

getAllStudents()
        .then(res => res.json())
        .then(data => console.log(data))

So in the first example the called function console.log somehow implicitly knows it should take the data as parameter and show it in the console. That function is not even called with console.log()

Could you please tell me what this shortcut concept is called? I would like to read more about it, but I don’t know how exactly I should use it.

Answer

It is called “point free style” or tacit programming.

See e.g. https://en.wikipedia.org/wiki/Tacit_programming

Edit: The above case may not exactly be tacit programming, as that involves defining functions without explicitly mentioning their arguments (“tacit” = “implicit”), and here you are merely using an already defined function as an argument. But a function definition

const log = console.log

would be a tacit function, as opposed to

const log = data => console.log(data)

so the concept is at least closely related to the difference in the question.