Callback Function Javascript
-
What is a Callback function?
- A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
-
Example 1:
function greeting(name) {
console.log("Hello " + name);
}
function processUserInput(callback) {
var name = "Chaitanya";
callback(name);
}
processUserInput(greeting); // Greeting is the callback function
- Example 2:
// the anonymous function is the callback function
document.addEventListener("click", function () {
})