
SEƦVING THƦOUGH CODE || EVEƦY LINE AN OFFEƦING
When you start learning JavaScript, functions seem very straightforward — you define them, call them, and they execute.
But after a while, you’ll notice something unusual:
functions being passed into other functions.
That’s where things start to get interesting — and where callbacks come in.
1. Functions Are Not Special (They’re Just Values)
In JavaScript, functions behave like any other value.
Just like you can store numbers or strings, you can also:
Assign functions to variables
Pass them as arguments
Return them from other functions
Example:
function hello() {
console.log("Hello!");
}
function execute(fn) {
fn();
}
execute(hello);
Output:
Hello!
Here, we didn’t call hello() directly — we passed it and let another function run it.
That’s the key idea behind callbacks.
2. What Exactly is a Callback?
A callback is simply:
--A function given to another function, to be executed later.
Example:
function getUserData(callback) {
const user = "Rushikesh";
callback(user);
}
function displayUser(name) {
console.log("Welcome " + name);
}
getUserData(displayUser);
Output:
Welcome Rushikesh
Here:
displayUser→ callbackgetUserData→ function using the callback
3. Why Callbacks Even Exist
Now the important question:
Why not just call functions normally?
Because JavaScript deals with things that don’t happen instantly, like:
API requests
File operations
User clicks
Timers
Instead of blocking everything, JavaScript says:
--“Continue running… I’ll notify you when this task is done.”
And that notification is handled using a callback
4. Callbacks in Action (Async Behavior)
Let’s look at a simple timer:
console.log("Begin");
setTimeout(() => {
console.log("Executed later");
}, 2000);
console.log("Finish");
Output:
Begin
Finish
Executed later
Why?
JavaScript doesn’t wait for 2 seconds
It continues execution
Then runs the callback when time is up
5. Passing Logic Dynamically
Callbacks allow us to make functions flexible.
function operate(a, b, action) {
return action(a, b);
}
const sum = (x, y) => x + y;
const product = (x, y) => x * y;
console.log(operate(3, 4, sum)); // 7
console.log(operate(3, 4, product)); // 12
Instead of writing multiple functions, we pass behavior as needed
6. Where You See Callbacks Daily
Callbacks are everywhere in JavaScript:
1. Timers
setTimeout(() => console.log("Done"), 1000);
2. Arrays
[10, 20, 30].forEach(num => console.log(num));
3. Events
button.addEventListener("click", () => {
console.log("Clicked!");
});
In all these cases:
--A function is passed and executed later
7. The Dark Side: Callback Hell
Callbacks are powerful… but they can become messy.
Example:
setTimeout(() => {
console.log("First");
setTimeout(() => {
console.log("Second");
setTimeout(() => {
console.log("Third");
}, 1000);
}, 1000);
}, 1000);
This creates a structure like:
First
Second
Third
This is called callback hell.
8. Why It’s a Problem
Code becomes deeply nested
Hard to read
Hard to debug
Hard to maintain
Instead of clean steps:
A → B → C
You get:
A
└── B
└── C
9. Modern Solutions (Just a Glimpse)
To fix this mess, JavaScript introduced:
Promises
async/await
They make asynchronous code look clean and readable again.
But here’s the truth:
--Callbacks are the foundation of all of it.
Final Thoughts
Callbacks may feel confusing at first, but the core idea is simple:
Functions are values
Values can be passed
Passed functions can run later
Once you understand this, asynchronous JavaScript starts making sense.
Want More…?
I post Blogs and related content on the following platforms:





