What are callbacks in Node.js?
Why Interviewers Ask This
This is a classic screening question for Node.js roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
A callback is a function passed as an argument to another function, to be called when an asynchronous operation completes. Node.js uses the error-first callback convention (also called Node.js callback style or errback): the first argument of the callback is always an error object (or null if no error), and subsequent arguments are the result data. Example: fs.readFile("file.txt", "utf8", (err, data) => { if (err) throw err; console.log(data); });. This pattern allows Node.js to handle errors consistently across all asynchronous operations. The main drawback of callbacks is callback hell — deeply nested callbacks for sequential async operations — which makes code hard to read and maintain. Promises and async/await were introduced to solve this problem.
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Node.js answers easy to follow.