Callback Functions in JavaScript

Last Updated: 22nd October 2025


A callback function is a function passed as an argument to another function.
It is called (or “executed”) after the completion of that function’s task.

Hinglish Tip 🗣: Callback ko aise samjho — ek function ko doosre function ke andar bheja jaata hai taaki woh kaam hone ke baad call ho sake.


✏️ Syntax

function mainFunction(callback) {
  console.log("Main function executing...");
  callback(); // calling the passed function
}

function greet() {
  console.log("Hello from Callback!");
}

mainFunction(greet);

Example 1:

function display(result) {
  console.log("Result:", result);
}

function add(a, b, callback) {
  let sum = a + b;
  callback(sum);
}

add(5, 10, display);

Example 2:

function greetUser(callback) {
  console.log("Preparing greeting...");
  callback();
}

greetUser(function () {
  console.log("Hello, Welcome!");
});