30

What is the difference between these two javascript function calls?

(function(){alert("foo")})()

versus this:

(function(){alert("foo")}()) 
keparo
  • 33,450
  • 13
  • 60
  • 66
Dexy_Wolf
  • 989
  • 3
  • 13
  • 25
  • I think this both will give the Same meaning, in other words both are same. – Sai Kalyan Kumar Akshinthala Jul 11 '11 at 04:38
  • The difference is only minor and mostly of style. Some prefer to have the formal parameter list (aka "call operator") inside the grouping operator (like Douglas Crockford), others like it outside. There is no practical difference - try [Ben Alman's Immediately-Invoked Function Expression](http://benalman.com/news/2010/11/immediately-invoked-function-expression/). – RobG Jul 11 '11 at 05:42

2 Answers2

28

This is done for readability.

There isn't a real functional difference between the two examples you've given, but both of them are very close to a simple function declaration, which is different. The parenthesis are added for readability, in order to distinguish them.

Here is what each of your snippets do:

In the first of your two snippets, the first parenthesis will be evaluated as the value of the enclosed function. Then this value will be called as a function. So ultimately the function will be executed, which is probably what you care about.

In your second snippet, the outer parenthesis will be evaluated as containing a function which is declared inline and immediately executed. Again, the function will be executed, which is still probably what you care about.

Both of these will execute the same function, so there won't be any significant difference.

The difference between a snippet like yours and a simple function declaration:

The functions you've given are also identical to the following. I've just added a function name and assigned the return value for syntactical accuracy, which you can ignore for now.

// javascript...
var val = 
  function myFooFunc () { 
    alert("foo"); 
  }();

However, this would be easily mistaken for a simple function declaration, which is different:

// javascript...
function myFooFunc () { 
  alert("foo"); 
}

Notice that the only real difference here is that this last function declaration is not executed immediately. The others are. So that is a very different behavior (the simple declaration may be executed later if it is called by name, or it may not ever execute at all). It's often hard to see that difference in the syntax right away, however, especially if the function body grows to be very long and requires scrolling on the screen.

Why are functions executed immediately?

When a function is immediately executed after it is declared, the value is often being returned to something (it may be part of an assignment statement). Sometimes the function is being executed right away because it contains inner functions and is being used to provide functional scope to the inclosed statements.

Essentially, people wrap parenthesis around the "executed immediately" form (both of your snippets, and the first one of my two) in order to give a visual cue to other developers that the function is being called immediately. It's just easier to read, since you might not catch the parenthesis until you got to the end of the function (or notice them at all).

keparo
  • 33,450
  • 13
  • 60
  • 66
  • I get syntax error on your first snippet. But when I put variable in front of first snippet function, it's working fine. – Dexy_Wolf Jul 11 '11 at 05:32
  • 2
    @Dejan - that's because the first example **is** a syntax error. A function declaration can't be followed by a grouping operator. The leading "(" before the *function* keyword changes it to a function expression, and of course needs a matching closing ")" at the end. Once changed to a function expression, the name becomes optional. – RobG Jul 11 '11 at 05:37
  • Yes, I had removed the outer parenthesis in my first snippet in order to draw more attention to the difference between it and my second one. However, given your comment, I think it may ultimately be more of a distraction if the example doesn't run as a stand-alone snippet. To remedy this, I've made a small edit to the first snippet which hopefully does not distract too much from the point, but allows the first snippet to be completely valid. – keparo Jul 12 '11 at 00:03
8

They both have similar behaviors.

The parentheses encapsulating the function declaration tell the JavaScript engine to execute the code immediately after it's parsed. In the first example, you're creating a function object then invoking it with the parentheses that follow. In the second example, you are telling the JavaScript engine to create the function object and invoke it immediately.

Example:

// creates a function object
var f1 = (function() { alert('foo'); }); 

// creates a function object and executes it immediately
var f2 = (function() { alert('foo'); }()); 

The difference is that f1 gives you a function object. f2 creates and invokes an anonymous function.

csano
  • 13,266
  • 2
  • 28
  • 45
  • 7
    The grouping operator is not required in either example, it is used for readability. The difference is that the first assigns the function to the variable, the second assigns the result of calling the function. – RobG Jul 11 '11 at 05:39