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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.