How to use callback function in JavaScript

2 Answers

0 votes
function print(result) {
    console.log(result); 
}
 
function calc(a, b, aCallback) {
    let sum = a + b;
  
    aCallback(sum);
}
 
calc(6, 9, print);

  
  
  
/*
run:
  
15
  
*/

 

 



answered May 19, 2021 by avibootz
edited Jun 5, 2024 by avibootz
0 votes
function afunction(name) {
    console.log('Hello ' + name);
}

function getUserInput(callback_function) {
    let name = prompt('Please enter your name: ');
    
    callback_function(name);
}

getUserInput(afunction);
 
 
 
/*
run:
 
Please enter your name: Dumbledore
Hello Dumbledore
 
*/

 



answered Jun 5, 2024 by avibootz
...