A callback is a function you pass into another function so it can be called later, usually to handle a result, an event, or a completed task.

Quick scoop

In programming, callbacks let one piece of code “call back” into your code at the right moment. They’re common in JavaScript for things like button clicks, timers, and array methods such as map, forEach, and setTimeout.

Simple example

js

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

function runCallback(cb) {
  cb("Sam");
}

runCallback(greet);

Here, greet is the callback because runCallback receives it and invokes it later.

Why people use them

  • To react to events like clicks or form submits.
  • To run code after an operation finishes, such as a timer or async task.
  • To make functions more reusable and flexible.

One-line meaning

If you pass a function to another function and that second function runs it for you, that function is a callback.