im learning javascript, and i have been following some video tutorial on youtube
this is the original code
function add(first, second, callback){
console.log(first+second);
callback();
}
function logDone(){
console.log("done");
}
add(2,3,logDone);
and the result of code on above is
5 main.js (line 4)
done main.js (line 9)
and i make slight change to the code into this
function add(first, second, callback){
console.log(first+second);
callback;
}
function logDone(){
console.log("done");
}
add(2,3,logDone());
and the result is this
done main.js (line 9)
5 main.js (line 4)
my question are:
could you explain to me why i got the result pile up that way?,
and what the difference if we call a function with the bracket () and without the bracket ()?
A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.
The difference between a callback function and any other function is how it's used. A callback function is specifically built to be used as argument of another function.
Example: Using a Callback FunctionThe sayName() function is passed as an argument to the greet() function. The setTimeout() method executes the greet() function only after 2 seconds. However, the sayName() function waits for the execution of the greet() function.
A Callback is a function that is to be executed after another function has finished executing — hence the name 'call back'.
Explanation of the first snippet
function add(first, second, callback) {
console.log(first + second);
callback(); // run whatever the "callback" function is
}
function logDone() {
console.log("done");
}
add(2, 3, logDone); // pass in a function (not an invocation of a function) the
// function isn't run here
Explanation of the second snippet
function add(first, second, callback) {
console.log(first + second);
callback; // display the value of whatever "callback" is
}
function logDone() {
console.log("done");
}
add(2, 3, logDone()); // run "logDone" and then pass the result (which in this
// case is undefined) into add
As you can see, the first code snippet doesn't actually run the callback
until in the add
function, whereas the second snippet runs the callback
before add
so that it can pass whatever comes back from logDone
into add
.
Perhaps it becomes more clear to you when I alter the logDone
declaration to this:
var logDone = function() {
console.log("done");
}
The identifier logDone
basically is just a variable that references a function. To execute (also: call, or: invoke) the function you add parentheses: logDone()
.
So, in your first example you are merely passing the function itself as the third argument to add()
, which then gets executed inside add()
, with callback();
.
In your second example, however, you are executing the function with logDone()
immediately, which results in the return value of logDone()
1 being passed as the third argument to the add()
call. In other words, first logDone
is executed (resulting in the first log message), then add
is executed (resulting in the second log message).
Furthermore, the statement callback;
, inside add
, does not do anything. And if you would have left the parentheses just like in your first example, it would result in an error, because undefined
2 is not a function.
1) which is undefined
in this case, because logDone()
does not explicitly return anything.
2) the value that was the result of the logDone()
call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With