Skip to main content

Callback function

A callback function is a function that is passed as an argument to another function and is called later, once that function finishes its work.

Callbacks are usually used to perform actions after an event, an operation, or an asynchronous process (for example, loading data, a timer, or a click).

Example:

javascript
function fetchData(callback) { setTimeout(() => { const data = 'Data from the server'; callback(data); // call the passed function }, 1000); } fetchData(result => console.log(result)); // "Data from the server"

Summary: a callback is a way to pass behavior and control the order in which code runs (especially in asynchronous operations).

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.