Skip to main content

Command Palette

Search for a command to run...

# Callbacks in JavaScript: Why They Exist

Updated
1 min read
# Callbacks in JavaScript: Why They Exist
U

Building cool stuff in web

If you’ve been learning JavaScript, you’ve definitely seen the word callback.

At first it feels like:

Okay... it's a function... but why is it passed inside another function???


What is a Callback?

A callback is simply a function passed into another function.

function greet(name, callback) {
  console.log("Hello " + name);
  callback();
}

function sayBye() {
  console.log("Bye!");
}

greet("Umer", sayBye);

Why Callbacks Exist

1. Handling Asynchronous Code

  • API calls
  • Fetching data

2. Event Handling

  • Button clicks
  • User actions

3. Non-blocking Design

JavaScript can run other code while waiting.


Final Thoughts

Callbacks may feel confusing at first, but once you understand why they exist, everything becomes clear.

6 views