# Callback Function

> A function you give to other code. That code runs it later.

**Field:** Software Development  
**Category:** Programming Paradigms  
**Also known as:** Callback, Callbacks

## Think of it like

Think of it like leaving your phone number at a shop. They call you when your order is in.

## Definition

A callback is a function you hand to other code, so that code can run it later. You often pass one to a timer, a click handler or a download. When the time comes, the other code calls your function back. Callbacks were the first way to write async code in JavaScript. Many callbacks inside callbacks get hard to read, which is why promises came along.

## Example

Emma gives setTimeout a callback and a wait of 2 seconds. Then her function runs and shows a message.

## Example code

```javascript
setTimeout(() => console.log("Done!"), 2000);
```

The arrow function is the callback. It runs after 2 seconds.

## Why it matters

Callbacks are used for clicks, timers and file reads. List tools like map take one too.

## Common mistake

Nesting callbacks five deep, each waiting on the last. Use promises or async/await to flatten them.

## Three principles

- **Hand it over.** Other code holds it.
- **Run later.** When the moment comes.
- **Nesting hurts.** Deep stacks get messy.

## Related terms

- [Asynchronous Code](https://learn.benibix.com/term/development/asynchronous-code)
- [Promise (JavaScript)](https://learn.benibix.com/term/development/promise)
- [Higher-Order Function](https://learn.benibix.com/term/development/higher-order-function)
- [Browser Event](https://learn.benibix.com/term/development/browser-event)
- [First-Class Function](https://learn.benibix.com/term/development/first-class-function)

## Told apart from

- [Callback vs Promise](https://learn.benibix.com/compare/callback-vs-promise): A callback is a function you hand over to be run later. A promise is an object for a result to come, which you can chain with then. Promises avoid deep nests of callbacks.

---

Source: https://learn.benibix.com/term/development/callback
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
