JavaScriptEasy
What are callback functions and how are they used?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. They are commonly used for asynchronous operations like handling events, making API calls, or reading files. For example:
js
function fetchData(callback) {
// assume an asynchronous operation to fetch data
const data = { name: "John Doe" };
callback(data);
}
function handleData(data) {
console.log(data);
}
fetchData(handleData);
