Q:

callback function javascript

function greeting(name) {
  alert('Hello ' + name);
}

function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  callback(name);
}

processUserInput(greeting);
4
function add(a, b, callback) {
  if (callback && typeof(callback) === "function") {
    callback(a + b);
  }
}

add(5, 3, function(answer) {
  console.log(answer);
});
0
function greeting(name) {
  alert('Hello ' + name);
}

function processUserInput(callback , {
  var name = prompt('Please enter your name.');
  callback(name);
}}

processUserInput(greeting);
1
const message = function() {  
    console.log("This message is shown after 3 seconds");
}
 
setTimeout(message, 3000);
1
//Callback functions - are functions that are called AFTER something happened

  const foo = (number, callbackFunction) => {
    //first 
    console.log(number)
    
    //second - runs AFTER console.log() happened
    callbackFunction()
  }
  
1
function createQuote(quote, callback){ 
  var myQuote = "Like I always say, " + quote;
  callback(myQuote); // 2
}

function logQuote(quote){
  console.log(quote);
}

createQuote("eat your vegetables!", logQuote); // 1

// Result in console: 
// Like I always say, eat your vegetables!
0

New to Communities?

Join the community