Callbacks in JavaScript Explained Simply (For Beginners)

Ezinne Nwani is a Frontend Developer based in Lagos, Nigeria. She shares her journey through clear, beginner-friendly technical articles on web development.
Callbacks in JavaScript confused me a lot when I first encountered them.
I read articles and watched videos, but nothing really clicked. I could repeat the definition, yet I didn’t truly understand what was happening behind the scenes. If this sounds like you, you’re not alone.
In this article, I’ll explain callbacks in a simple, beginner-friendly way, focusing on what they are, why they exist, and how they’re used, without overcomplicating things.
Why Callbacks Matter in JavaScript
JavaScript runs code synchronously by default, meaning it executes one line at a time. But in real applications, we often need to:
wait for data from an API
respond to user actions
delay execution (timers)
Callbacks allow JavaScript to handle these situations by saying:
“Run this function later, when you’re ready.”
Understanding callbacks is important because they are the foundation of asynchronous JavaScript.
What Is a Callback?
A callback is a function that is passed as an argument to another function and then executed inside that function.
The function receiving the callback decides when it should run.
A Simple Real-Life Analogy
Imagine you’re at a coffee shop. You place an order, and the waiter takes it. The waiter doesn’t stand beside you until your coffee is ready. Instead, when the coffee is done, you’re notified to come pick it up.
In this scenario:
The waiter is the higher-order function
The notification is the callback
The callback runs after the main task (making the coffee) is complete
Callbacks with Named Functions
Let’s start with a simple example.
A normal function:
function sayHello() {
console.log("Hello!");
}
At this point, this is not a callback, it’s just a regular function.
A function that accepts another function:
function runLater(fn) {
fn();
}
The fn parameter means:
“I don’t know what function I’ll receive yet, but I’ll run it later.”
Passing the function:
runLater(sayHello); // Hello!
Here, sayHello becomes the callback, and "Hello!" is logged to the console.
A More Practical Example (Coffee Shop Scenario)
function completedOrder(order) {
console.log(`Your ${order} is ready`);
}
This function simply logs a message when an order is ready.
Now the higher-order function:
function receivedOrder(callback) {
const request = "tea";
callback(request);
}
The callback parameter again means:
“I don’t know what function this is yet, but I’ll call it when I’m ready.”
Calling the function:
receivedOrder(completedOrder); // Your tea is ready
This logs:
Your tea is ready
Anonymous Callback Functions
Callbacks don’t always need names.
A common example is setTimeout, which executes a function after a delay:
setTimeout(function() {
console.log("This runs after 3 seconds");
}, 3000);
Here:
setTimeout is the higher-order function
The anonymous function is the callback
Another Simple Callback Example
function doHomework(callback) {
callback();
}
doHomework(function() {
console.log("Homework done");
});
If you look closely:
callback === function() {
console.log("Homework done");
}
The function is passed in and then executed inside doHomework.
When to Use Callbacks
Callbacks are useful when:
You want to control the flow of your code
You’re handling asynchronous tasks like API calls or event listeners
You’re creating higher-order functions
Problems with Callbacks
As useful as callbacks are, they can cause issues:
Callback hell — deeply nested callbacks that make code hard to read
Error handling — managing errors across nested callbacks can get messy
Because of these problems, JavaScript introduced Promises and async/await, which provide cleaner ways to handle asynchronous code. Understanding callbacks makes those concepts much easier to learn later.
Final Thoughts
If callbacks feel confusing right now, that’s completely normal. They confused me too. Once I understood that callbacks are simply functions passed around and executed later, everything started to make sense.
Thank you so much for reading! ❤️



