"When you're done with your current work, call me back."
What are Callbacks in JavaScript?
A callback is a function that is passed as an argument to another function and is executed later, usually after a task or operation has completed.
They allow JavaScript to continue executing other code instead of waiting for the operation to complete.
Why Are Callbacks Needed?
Callbacks are widely used in JavaScript. for handling asynchronous operations. When an asynchronous task finishes, the Event Loop executes the corresponding callback, enabling non-blocking behavior.
For example:
callback.js
function orderPizza(callback) {
console.log("Preparing pizza...");
callback();
}
orderPizza(() => {
console.log("Pizza is ready!");
});
// Output:
// Preparing pizza...
// index.js:7 Pizza is ready!