Course contentsShow
← JavaScript
Lesson 1221 of 2,72328. CallbacksFree lesson

What is a Callback?

A callback is a function passed as an argument to another function, to be invoked later.

What is a Callback?

You'll learn the fundamental definition of a callback function and why it's central to asynchronous JavaScript.

The Core Idea

A callback is simply a function that you pass as an argument to another function, with the expectation that it will be invoked (called back) at some point in the future.

Think of it like leaving your phone number at a restaurant. You're not sitting at the table waiting — you've given them a way to "call you back" when your table is ready. Similarly, you give JavaScript a function to "call back" when something completes.

Why Callbacks Exist

In JavaScript's single-threaded world, callbacks let you say: "Here's what I want you to do when this operation finishes, but don't block everything else waiting for it."

The function receiving the callback decides when to invoke it. You're handing over control, trusting that function to execute your callback at the appropriate time.

Real-World Mental Model

Imagine asking a friend to text you when they arrive:

  • The favor: "Let me know when you get there"
  • The callback: The actual texting action they'll perform later
  • You continue your day: You don't freeze in place waiting — you go about your business until they call back

Callbacks work the same way — you define what should happen later, then JavaScript continues executing other code until it's time to invoke your callback.

Key Takeaway: A callback is a function you pass to another function to be executed later, enabling non-blocking, asynchronous behavior in JavaScript.