What is a Callback Function in JavaScript?
Learn what a callback function is in JavaScript, synchronous vs asynchronous examples, the error-first convention, and how callback hell led to Promises.
Expected Interview Answer
A callback is a function passed as an argument to another function, invoked later once that function completes some work or reaches a certain point. Callbacks are how JavaScript historically handled asynchronous operations before Promises and async/await existed.
Callbacks can be synchronous, like the function passed to Array.prototype.map that runs immediately for each element, or asynchronous, like the callback passed to setTimeout or a file read operation that runs later after some event completes. The pattern works because functions are first-class values in JavaScript, so they can be stored in variables and passed around just like any other data. Historically, chaining many asynchronous callbacks led to deeply nested, hard-to-read code known as callback hell, which motivated the creation of Promises and later async/await as cleaner alternatives. Error-first callback conventions, common in Node.js, pass an error as the first argument so the caller can check it before using the result.
- Enables higher-order functions like map, filter, and reduce
- Foundation for event-driven and asynchronous code
- Simple mental model: pass a function, call it later
- Still widely used in Node.js APIs and event listeners
- Basis that Promises and async/await were built to improve on
AI Mentor Explanation
A callback is like a bowler handing the umpire a note that says 'signal a six the moment the ball clears the boundary', instead of the bowler personally watching and reacting themselves. The umpire runs that instruction exactly when the triggering event happens, not before.
Step-by-Step Explanation
Step 1
Pass a function as an argument
Because functions are first-class values, you can pass one function into another as a plain argument, called the callback.
Step 2
Outer function does its work
The receiving function performs its own logic, whether synchronous like mapping an array or asynchronous like a timer.
Step 3
Callback is invoked at the right moment
The outer function calls the callback exactly when appropriate, passing it any relevant results or error.
Step 4
Error-first convention (Node.js)
Many Node APIs call the callback with an error as the first argument, so you check err before using the result.
Step 5
Nesting leads to callback hell
Chaining many async callbacks sequentially creates deeply nested code, which Promises and async/await were built to solve.
What Interviewer Expects
- Distinguishes synchronous vs asynchronous callbacks with examples
- Explains that functions being first-class values enables callbacks
- Can describe the error-first callback convention in Node.js
- Knows what callback hell is and why it happens
- Connects callbacks historically to the rise of Promises
Common Mistakes
- Assuming all callbacks are asynchronous
- Confusing a callback with a Promise
- Not checking the error argument in Node-style callbacks
- Writing deeply nested callbacks instead of refactoring
- Forgetting synchronous examples like array methods use callbacks too
Best Answer (HR Friendly)
“A callback is a function you hand to another function, telling it 'run this when you're done' or 'run this for each item.' It's a simple way to say what should happen next without freezing the rest of the program while waiting.”
Code Example
// Synchronous callback
const doubled = [1, 2, 3].map((n) => n * 2);
console.log(doubled); // [2, 4, 6]
// Asynchronous callback
function loadData(callback) {
setTimeout(() => {
callback(null, { id: 1, name: 'SkillVeris' });
}, 100);
}
loadData((err, data) => {
if (err) return console.log('Error:', err.message);
console.log(data.name); // SkillVeris
});Follow-up Questions
- What is callback hell and how do Promises solve it?
- What is the error-first callback convention in Node.js?
- How do synchronous callbacks differ from asynchronous ones?
- Can you convert a callback-based function into a Promise-based one?
- How does a callback relate to a higher-order function?
MCQ Practice
1. What makes callbacks possible in JavaScript?
Because functions are first-class values, they can be passed as arguments and invoked later, which is the essence of a callback.
2. What convention do many Node.js APIs use for callbacks?
Node.js APIs commonly use the error-first convention, passing an error as the first argument to the callback.
3. What problem is commonly called 'callback hell'?
Callback hell refers to deeply nested, hard-to-read code that results from chaining many asynchronous callbacks sequentially.
Flash Cards
Define a callback function. — A function passed as an argument to another function, invoked later at an appropriate point.
Give an example of a synchronous callback. — The function passed to Array.prototype.map, which runs immediately for each element.
What is the error-first callback convention? — A Node.js pattern where the callback's first argument is an error (or null), checked before using the result.
What motivated Promises over plain callbacks? — Avoiding callback hell — deeply nested, hard-to-read async code from chained callbacks.