Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Callback

Колбэк

A function you give to another piece of code so it can call it when needed. For example, a browser can call a function you registered when someone clicks a button.

Also known as: функция обратного вызова

Who calls the function?

A callback lets you supply a piece of behavior without having to decide exactly when it runs. A sorting function, for example, can handle all the work of ordering a list while your comparison callback tells it which of two items should come first. The sorting function chooses when to make comparisons; you supply the rule it uses.

“Callback” describes the role a function plays, not how it is declared. You can pass a named function or write one inline. The function receiving it is a higher-order function: it takes a function as an argument.

Timing depends on the caller

A callback can run immediately as part of the operation, or it can be saved for later. In JavaScript, filter calls its predicate synchronously and uses the result to decide whether to keep each element. A browser event listener, on the other hand, is registered now and can be called whenever the event occurs.

const names = ['Ari', 'Maya', 'Sam'];
const longNames = names.filter(name => name.length > 3);

setTimeout(() => console.log('timer'), 0);
console.log(longNames); // ['Maya'], then 'timer'

The predicate has finished running by the time filter returns. The timer callback cannot run until the current code finishes, even with a delay of 0. Passing a callback does not create a new thread or guarantee that it will run asynchronously.

Read the callback contract

The API determines which arguments the callback receives, whether it uses the return value, and how often the callback may run. The return value matters to filter but is ignored by setTimeout. An event handler may run repeatedly, or never if the event does not occur. In many Node.js APIs, a completion callback takes (err, result): check err before using the result. That is an API convention, not a rule for every JavaScript callback.

A common mistake is to write setTimeout(report(), 1000) when you mean setTimeout(report, 1000). The parentheses call report while the arguments are being evaluated. Without them, you pass the function itself so the timer can call it later.

How it comes up in interviews

Interviewers often ask you to trace the order of log messages or explain why returning a result inside a timer callback does not return it from the enclosing function. Look for two separate events: when the outer call returns and when the callback runs. For a one-off asynchronous result, a Promise gives the caller something it can await. It can also flatten deeply nested completion callbacks, often called callback hell. For repeated events or synchronous collection operations, a callback is still a natural fit.