DEV Community

Chandru
Chandru

Posted on

Callback Function in JavaScript

A callback function is a function that is passed as an argument to another function and is called later when needed.

Here is a simple example:

function greet(name) {
  console.log("Hello " + name);
}

function processUser(callback) {
  callback("John");
}

processUser(greet);
Enter fullscreen mode Exit fullscreen mode

Output:

Hello John
Enter fullscreen mode Exit fullscreen mode

Here, greet is the callback function because we pass it to processUser().

The processUser() function then calls it using callback("John").

In simple words:

A callback is a function passed to another function to be executed later.

Top comments (0)