simple callback function in javascript code example
Example 1: javascript callback
function greeting(name) {
alert('Hello ' + name);
}
function processUserInput(callback) {
var name = prompt('Please enter your name.');
callback(name);
}
processUserInput(greeting);
Example 2: javascript callback
function newCallback(callback) {
callback('This can be any value you want to return')
}
function actionAferCallback (callbackData) {
console.log(callbackData)
}
function requestCallback() {
newCallback(actionAferCallback)
}
Example 3: javascript callback function
const message = function() {
console.log("This message is shown after 3 seconds");
}
setTimeout(message, 3000);
Example 4: callback functions javascript
function isOddNumber(number) {
return number % 2;
}Code language: JavaScript (javascript)