Perhaps you will become more clear when I change the logDone to this:
var logDone = function() { console.log("done"); }
The logDone identifier is basically just a variable that refers to a function. To execute (also: call or: call) the function that you add parentheses: logDone() .
So, in the first example, you simply pass the function as the third argument to add() , which is then executed inside add() , with callback(); .
In your second example, however, you execute the function with logDone() immediately, which causes the return value of logDone() 1 to be passed as the third argument to the add() call. In other words, the first logDone (the first log message appears as a result), then add is executed (the second log message appears as a result).
In addition, a callback; statement callback; , inside add , does nothing. And if you would leave parentheses as in the first example, this would lead to an error, because undefined 2 is not a function.
1), which in this case is undefined , because logDone() does not explicitly return anything.
2) the value that was the result of calling logDone() .
Decent dabbler
source share