7

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:

  1. could you explain to me why i got the result pile up that way?,

  2. and what the difference if we call a function with the bracket () and without the bracket ()?

3
  • 1
    Because without the brackets, you're not yet calling it - you just pass the function as an argument. It gets called in the callback(); line. Commented Mar 1, 2015 at 16:05
  • As you called the function logDone by adding the brackets in the second code snippet you get the console log first Commented Mar 1, 2015 at 16:06
  • 1
    JavasScript evaluates arguments first. So if you have foo(bar()), bar is called first and its return value is passed to foo. Is btw is how most programming languages work. Commented Mar 1, 2015 at 17:13

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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 undefined2 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.

Comments

3

and what the difference if we call a function with the bracket () and without the bracket ()?

If you have () then you call it. If you don't, then you don't.

could you explain to me why i got the result pile up that way?,

In the first example you pass a function to add and then the add function calls it.

In the second example, you call the function and pass its return value (undefined) to add which then mentions it in a statement but doesn't do anything with it.

Comments

3

When you do func([args=optional]), func is getting invocated or called.

and what the difference if we call a function with the bracket () and without the bracket ()?

We call the function only when we do ()(arguments optional). When a function is without parenthesis, you're just using it's reference.

By storing a reference do a function in a variable, you can call it later, that's what callback is doing.

In the second snippet, since nothing is returned, callback will have undefined. Try calling it by doing callback() in the second snippet, and you should see an error stating,

undefined is not a function.

Comments

3

In the first example, you are passing a function as a parameter.
In your second example, you are passing a functions result after invocation as a parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.