Callbacks in JavaScript: Why They Exist

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
In this article we'll explore the concept of callbacks in JavaScript along with simple code examples that will help us understand it more easily.
A callback function is a function that is passed into another function as an argument and then executed later inside that function. In JavaScript, functions are treated like normal values. This means we can store them in variables, pass them to other functions, and even return them from functions. Because of this feature, one function can receive another function and decide when to execute it. The function that gets passed is called a callback function.
function greet(name, callback) {
console.log("Hello " + name);
callback();
}
function sayBye() {
console.log("Bye!");
}
greet("Sam", sayBye);
In this example, sayBye is passed into the greet function and executed inside it. Since it is called back by another function, it is known as a callback.
Callbacks are heavily used in asynchronous programming. JavaScript runs on a single thread, which means it can only perform one operation at a time. However, many tasks in web development take time, such as fetching data from an API, reading files, or waiting for timers.
If JavaScript waited for each of these tasks to finish before moving forward, the application would freeze. Instead, JavaScript starts the task and continues executing other code. Once the task finishes, a callback function is executed. This allows the program to remain responsive.
A simple example is using setTimeout.
console.log("Start");
setTimeout(function () {
console.log("Timer finished");
}, 2000);
console.log("End");
The function passed to setTimeout is the callback. JavaScript schedules it to run after 2 seconds while continuing to execute the rest of the code. That is why the output becomes:
Start
End
Timer finished
One powerful concept in JavaScript is that functions can be passed as arguments. Since functions are values, they can be sent into other functions to control how those functions behave.
function processNumber(num, operation) {
operation(num);
}
function double(n) {
console.log(n * 2);
}
function square(n) {
console.log(n * n);
}
processNumber(5, double);
processNumber(5, square);
In this example, the processNumber function remains the same, but its behavior changes depending on the callback that is passed. This makes code more flexible and reusable.
Callbacks appear in many real-world JavaScript situations. One of the most common examples is event handling. When a user clicks a button, types in an input field, or scrolls a page, a callback function runs in response to that event.
button.addEventListener("click", function () {
console.log("Button clicked");
});
The function passed to addEventListener is a callback that runs whenever the click event happens.
Callbacks are also used in:
Timers like setTimeout and setInterval
API requests
File operations in Node.js
Handling user interactions in the browser
Because asynchronous tasks are everywhere in web development, callbacks became a fundamental part of JavaScript programming.
Although callbacks are useful, they can sometimes lead to messy code when multiple asynchronous operations depend on each other. This situation is known as callback nesting, often referred to as callback hell.
loginUser(function () {
getUserProfile(function () {
getPosts(function () {
console.log("All data loaded");
});
});
});
Here each step waits for the previous one to complete, which forces the code into deeper levels of nesting. As the number of operations increases, the code becomes harder to read, maintain, and debug.
Because of this problem, modern JavaScript introduced Promises and later async/await, which allow developers to handle asynchronous operations in a cleaner and more structured way.
And saved our eyes and brain!