Callback Function (JavaScript)

A callback is a function passed as an argument to another function, to be called ("called back") later. Callbacks power events, timers, and array methods.

JavaScript

Instead of returning a value, many JavaScript functions accept a callback and run it when they are ready — for example after a timer finishes or an array element is processed.

Callbacks were the original way to handle asynchronous work before promises and async/await arrived.

Examples

JavaScript

function greet(name, callback) {
  callback("Hello, " + name);
}
greet("Sam", message => console.log(message));

Output:

Hello, Sam

Learn it in a lesson

See also

Back to the Dictionary