Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6118773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:28:31+00:00 2026-05-23T15:28:31+00:00

What is the difference between these two javascript function calls? (function(){alert(foo)})() versus this: (function(){alert(foo)}())

  • 0

What is the difference between these two javascript function calls?

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

versus this:

(function(){alert("foo")}()) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:28:32+00:00Added an answer on May 23, 2026 at 3:28 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between these two functions? 1: $(document).ready(function myfunc() { function dosomething()
What is the difference between these two? collapse: function(fold) { ... ... } and
What is actually the difference between these two casts? SomeClass sc = (SomeClass)SomeObject; SomeClass
What is the difference between these two pieces of code type IInterface1 = interface
I've been researching the difference between these two patterns. I understand that facade encapsulates
When accessing values from an SqlDataReader is there a performance difference between these two:
In Access, what is the difference between these two statements? DBEngine.BeginTrans and DBEngine.Workspaces(0).BeginTrans The
I'm just looking for a simple, concise explanation of the difference between these two.
In C#, what is the difference (if any) between these two lines of code?
Is there a difference (performance, overhead) between these two ways of merging data sets?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.