What is a callback function in JavaScript?

A callback is a function passed as an argument to another function, which calls it later.

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

process("Robin", () => console.log("Done"));

setTimeout(() => console.log("After 1 second"), 1000);

Output:

Tutorials Class - Output Window

Hello Robin
Done
After 1 second

Callbacks are used for events, timers and array methods like map. Many nested callbacks become hard to read (callback hell), and promises solve that.


Learn more about the similar topics:
Tutorials
JavaScript Callbacks and Promises
JavaScript Async Await and Fetch API
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is a callback function in JavaScript?
What is async and await in JavaScript?
What is a Promise in JavaScript?
What is the event loop in JavaScript?