1/*
2A callback function is a function passed into another function
3as an argument, which is then invoked inside the outer function
4to complete some kind of routine or action.
5*/
6function greeting(name) {
7 alert('Hello ' + name);
8}
9
10function processUserInput(callback) {
11 var name = prompt('Please enter your name.');
12 callback(name);
13}
14
15processUserInput(greeting);
16// The above example is a synchronous callback, as it is executed immediately.
1function startWith(message, callback){
2 console.log("Clearly written messages is: " + message);
3
4 //check if the callback variable is a function..
5 if(typeof callback == "function"){
6 callback(); //execute function if function is truly a function.
7 }
8}
9//finally execute function at the end
10startWith("This Messsage", function mySpecialCallback(){
11 console.log("Message from callback function");
12})
1function greeting(name) {
2 alert('Hello ' + name);
3}
4
5function processUserInput(callback) {
6 var name = prompt('Please enter your name.');
7 callback(name);
8}
9
10processUserInput(greeting);
1function add(a, b, callback) {
2 if (callback && typeof(callback) === "function") {
3 callback(a + b);
4 }
5}
6
7add(5, 3, function(answer) {
8 console.log(answer);
9});
1function greeting(name) {
2 alert('Hello ' + name);
3}
4
5function processUserInput(callback , {
6 var name = prompt('Please enter your name.');
7 callback(name);
8}}
9
10processUserInput(greeting);
1
2 function myDisplayer(some) { document.getElementById("demo").innerHTML
3 = some;}function myCalculator(num1, num2, myCallback) {
4 let sum = num1 + num2;
5 myCallback(sum);}myCalculator(5, 5, myDisplayer);
6