JavaScript — Callback function
Hello Guys, Welcome to my blog!
Today we are going to learn about Callback Functions in JavaScript at a very basic level.
If you’re just starting out with JavaScript, you might have already seen functions being passed into other functions. That’s what callbacks are all about! Don’t worry if it sounds confusing right now — we’ll break it down step by step with super simple examples so you can understand it easily.
Callback function — A callback function is simply a function that is passed as an argument to another function and is executed later by that function.
Get Saurabhhavaldar’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:-
function greetUser(name, callback) {
console.log("Hello " + name);
callback();
}
function sayThanks() {
console.log("Thanks for visiting!");
}
greetUser("Sourabh", sayThanks);Output:-
Hello Sourabh
Thanks for visiting!Explanation:-
Here, the greetUser function is called first with two arguments:
- A string
"Sourabh" - A callback function
sayThanks
Inside the greetUser function, we are executing the function that we passed as an argument. That’s why sayThanks runs after printing the greeting.
⚡ That’s it for today’s blog!
Hope this helped you understand callbacks at a beginner-friendly level. 🚀






