callback functions js code example
Example 1: javascript callback
/*
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.
*/
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
// The above example is a synchronous callback, as it is executed immediately.
Example 2: callback function js
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
Example 3: callback in js
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback , {
var name = prompt('Please enter your name.');
callback(name);
}}
processUserInput(greeting);
Example 4: js callback function
const add = (num1, num2) => num1 + num2;
const result = (num1, num2, cb) => {
return "result is:" + cb(num1, num2);
}
const res = result(12, 13, add);
Example 5: callback functions javascript
function isOddNumber(number) {
return number % 2;
}Code language: JavaScript (javascript)