What are callbacks in Node.js?

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.