I'm new to JavaScript and recently learned about callbacks via a tutorial. The instructor mentioned that callbacks are used everywhere in code amongst JavaScript developers. What I understand so far is that these callbacks store a function within a function to be executed at a later time, but wouldn't it be simpler and more efficient to store a value in a variable and create a function that logs your desired output? Can’t the callback alternative (typed below) be executed at a later time as well?
// Callback Example:
function callbackExample(name, callback){
log(callback(name));
}
callbackExample('Adee Otana', function(name){
return 'Hello ' + name;
});
// Alternative:
let name = 'Adee';
function callbackAlt(name){
log('Hello ' + name);
}
callbackAlt(name);