A callback is a function that is passed as an argument to another function and is executed after that function finishes its work. Callbacks are a core part of JavaScript, especially for handling asynchronous operations like timers, user input, and network requests.
Because functions in JavaScript are first-class citizens, you can store them in variables, pass them as parameters, and call them later — this is exactly what callbacks do.
setTimeout()).forEach() use callbacks to process each element.A typical pattern for using callbacks in JavaScript looks like this:
function doSomething(callback) {
// some work here...
callback(); // call the function passed in
}
function myCallback() {
console.log("Callback was called!");
}
doSomething(myCallback);
Here, myCallback is passed to doSomething without parentheses, meaning we are passing the function itself, not its return value. Inside doSomething, we can call it using callback() whenever we want.
A simple example where a callback is used to greet a user after getting their name.
function greet(name) {
alert('Hello, ' + name);
}
function processUserInput(callback) {
const name = prompt('Please enter your name:');
callback(name);
}
processUserInput(greet);
Here, greet is passed to processUserInput as a callback and is executed after the user enters their name.
Callbacks are commonly used with asynchronous functions like setTimeout() to delay execution.
function displayMessage() {
console.log("This message appears after 2 seconds");
}
setTimeout(displayMessage, 2000);
Sometimes you don’t need to reuse a callback, so you can define it anonymously.
setTimeout(function () {
console.log("Hello from an anonymous callback!");
}, 1000);
Callbacks can receive parameters, allowing you to pass data into them.
function greetUser(name) {
console.log("Hello, " + name);
}
function getUserInput(callback) {
const user = "Alice";
callback(user);
}
getUserInput(greetUser);
Array methods like forEach() use callbacks to perform operations on each item.
const numbers = [1, 2, 3, 4];
numbers.forEach(function (num) {
console.log(num * 2);
});
You can design functions that accept callbacks to customize what happens after a task.
function doTask(taskName, callback) {
console.log("Starting task: " + taskName);
callback();
}
function finish() {
console.log("Finished task!");
}
doTask("Download file", finish);
If you run the examples above in the browser console or an editor:
setTimeout examples will log messages to the console after a delay (1 or 2 seconds), demonstrating asynchronous behavior.forEach example will log 2, 4, 6, 8 to the console, doubling each number in the array using a callback."Starting task: Download file" and then "Finished task!", showing how callbacks can signal completion.forEach, map).if (typeof callback === "function") callback();).Click the button below to see a simple callback in action. The main function will perform a task and then call the callback to update the message.
Click the button to start the task and run the callback.
setTimeout with a callback to simulate a loading screen.forEach() to apply a callback to each item in an array.