Skip to content

What Is a Callback Function?

💡 What is a callback function?

A function passed as an argument to another function in order to be called from *inside* that function.

Callback functions are often initialised anonymously and inlined in the function call.

Initialising two functions: printToConsole and getGreeting. printToConsole takes a single argument and console.logs it. getGreeting takes a string and a callback function as arguments and calls the callback function with "Hello" prepended to the string. Calling getGreeting with "Maxim" and printToConsole as arguments outputs: Hello Maxim Initialising a getGreeting function that takes two arguments: a name and a callback. getGreeting calls the callback function with "Hello" prepended to the name. Calling getGreeting with name="Maxim" and an inlined anonymous callback function which prints the argument to the console results in "Hello Maxim" as output