Facebook Pixel

What Are Callback Functions in JavaScript?

A callback is a function passed as an argument to be called later. Here is how they work and why JS uses them everywhere.

What Are Callback Functions in JavaScript?

A callback is a function passed as an argument to another function, to be called later. Callbacks are the foundation of async JavaScript, event handling, and higher-order functions.

The Basic Idea

function process(value, callback) { const result = value * 2; callback(result); } process(5, (output) => console.log(output)); // 10 `` `process` takes a `callback` and calls it with the result. The caller decides what to do with the result by passing a different callback. ### Why JavaScript Uses Callbacks JS is single-threaded and synchronous. To handle async operations (timers, network, events) without blocking, it uses callbacks. The runtime does the work outside the engine and calls the callback when done. ### Common Callback Use Cases ```js // Timers setTimeout(() => console.log("later"), 1000); // Event listeners button.addEventListener("click", () => console.log("clicked")); // Array methods [1, 2, 3].map((n) => n * 2); // fetch fetch("/api").then((res) => res.json()); // Node.js fs.readFile("file.txt", (err, data) => { ... }); `` ### Synchronous Callbacks Not all callbacks are async. `map` calls its callback synchronously: ```js [1, 2, 3].map((n) => { console.log(n); // logs immediately return n * 2; }); `` ### Asynchronous Callbacks `setTimeout` calls its callback asynchronously (after the timer fires and the stack is empty): ```js setTimeout(() => console.log("async"), 0); console.log("sync"); // logs first `` ### The Callback Is a Closure Every callback closes over variables from where it was defined: ```js const multiplier = 3; [1, 2, 3].map((n) => n * multiplier); // [3, 6, 9] `` The `map` callback closes over `multiplier`. ### The Takeaway A callback is a function passed as an argument to be called later. JS uses callbacks for async operations (timers, events, fetch, Node.js APIs) and synchronous operations (map, filter, reduce). Every callback is a closure. Callbacks are the foundation of async JS, but deeply nested callbacks lead to callback hell (solved by promises).

A function passed as an argument to another function, to be called later. Callbacks are used for async operations (setTimeout, event listeners, fetch) and synchronous operations (map, filter, reduce).

Because JS is single-threaded and synchronous. To handle async operations without blocking, it passes callbacks to Web APIs (setTimeout, fetch) or Node.js APIs (fs). The runtime calls the callback when the operation completes.

No. map, filter, and reduce call their callbacks synchronously. setTimeout, event listeners, and fetch call their callbacks asynchronously. The timing depends on the calling function, not the callback itself.

Yes. Every callback closes over variables from where it was defined. This is how a map callback can access outer variables, and how a setTimeout callback can access state from the enclosing function.

Callback hell: deeply nested callbacks are hard to read, hard to debug, and prone to error handling issues. Promises and async/await were introduced to solve this by flattening the structure.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.