Callbacks
Discover how to pass functions as arguments to other functions and unlock powerful patterns.
Functions can receive other functions
Since functions are values in JavaScript (just like numbers and strings), you can pass a function as an argument to another function. A function that's passed as an argument is called a callback — because the receiving function will 'call it back' at the right time. This is one of the most powerful patterns in JavaScript and the foundation for event handling, timers, array methods, and asynchronous code. You've already been using callbacks without knowing it: `setTimeout`, `.forEach()`, `.map()`, and `.filter()` all take callbacks.
Callbacks are central to JavaScript's event-driven architecture. The browser uses callbacks for everything: click handlers, network responses, timers, and more. This pattern allows JavaScript to be 'non-blocking' — it can start a timer or network request, continue running other code, and then run the callback when the result is ready.