In this tutorial, you will learn about JavaScript callback functions with the help of examples.
In JavaScript, you can also pass a function as an argument to a function. This function that is passed as an argument inside of another function is called a callback function.
Example:
// function
function greet(name, callback) {
console.log('Hi' + ' ' + name);
callback();
}
// callback function
function callback() {
console.log('this is callback fundtion');
}
// passing function as an argument
greet('abhishek', callback);
OutPut:
Hi abhishek
this is callback fundtion
In the above program, there are two functions. While calling the greet() function, two arguments (a string value and a function) are passed.
The callback() function is a callback function.
I’m Abhishek, a DevOps, SRE, DevSecOps, and Cloud expert with a passion for sharing knowledge and real-world experiences. I’ve had the opportunity to work with Cotocus and continue to contribute to multiple platforms where I share insights across different domains:
-
DevOps School – Tech blogs and tutorials
-
Holiday Landmark – Travel stories and guides
-
Stocks Mantra – Stock market strategies and tips
-
My Medic Plus – Health and fitness guidance
-
TrueReviewNow – Honest product reviews
-
Wizbrand – SEO and digital tools for businesses
I’m also exploring the fascinating world of Quantum Computing.
[…] CallBack Function in JavaScript […]