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

  • SEARCH
  • Home
  • 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 8637519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:29:49+00:00 2026-06-12T10:29:49+00:00

What is the semantic difference between these two snippets of code? Example 1 var

  • 0

What is the semantic difference between these two snippets of code?

  • Example 1

    var x = (function () {} ());

  • Example 2

    var x = (function () {} )();

The difference in writing is where we put our parenthesis pair () which tells the JavaScript interpreter to execute the code block at once. Technically, there is no difference. If I may rephrase the wording of my question and eliminate the word “semantic”, then the new question has already been answered and elaborated upon here: https://stackoverflow.com/a/6645915/1268003

But, the semantic part is my dilemma and what I hope this thread could shine some light on. Due to it being “semantic”, there can be no objective answer and my question is admittedly on the borderline of not being valid according to the FAQ of Stackoverflow. However, I can argue for the validity of this question which just might happen in the comments. For now, lets move on.

Defining the problem

The absolute vast majority of all JavaScripters out there seems to use example 2. Actually, I haven’t yet found one exception to that rule (or where have I been lately?). jQuery plugin developers in particular, write code like so:

  • Example 3 (see example of an example)

    (function ($) {} )(jQuery);

My bible, JavaScript: The Definitive Guide , doesn’t use this practice at all – not once. Author David Flanagan sticks to example 1 (page 249 is the place where author comments this practice).

Never mind which construct, added parenthesis all have one and only one intention: To maximize readability. To clarify. Then, may I ask, clarify what?

My reasoning

When writing a closure in JavaScript, we’re defining a local variable in a function that is not supposed to be accessible from code outside. For the most part and due to context, our function is an anonymous one that isn’t bound to an identifier. Like so:

  • Example 4

    function () { var local = "Local variable!"; }

Again due to context and what is usually the practice, we want to execute that snippet of code at once. So we throw in a couple of parenthesis. This will tell the JavaScript interpreter to run the function:

  • Example 5

    function () {} ()

To clarify this in code, best practice is to add a couple of parenthesis, in a way this also clarifies the closure:

  • Example 6

    (function () {} ())

This is an exact replica of example 1. My reasoning thus gives that example 1is the way we “should” write code. Whenever I choose to use example 2, I only clarify the use of a closure, not the fact that our anonymous function is executed at once. But by the smallest of efforts to place our clarification-parenthesis on the outside of the entire code snippet, we can catch both.

Yes I can critique my reasoning. In example 3, I find it appealing to think “hey, outside reference is copied to an alias on the inside”. The alternative:

  • Example 7

    (function ($) {} (jQuery))

..isn’t as clear in this particular regard. However, example 2 doesn’t use this pass-by-value technique. Personally, for the most part when I write closures, I don’t have to pass anything in. And still, if we are allowed to be fundamentalist about it, the jQuery plugin practice never clarifies the immediate execution of our anonymous function.

Example 1 users

If my reasoning holds true, why is it then that the entire community out there use example 1? Is their sole intention to only cast one of two clarifications? Maybe just maybe, they don’t even have a purpose but are following the lead of a mere tradition of which purpose we might not be able to explain? I haven’t yet written a jQuery plugin, or used example 2 because I began my career reading the bible. But if you are among the many who use example 1 and have a reasoning for it, please share =)

  • 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-06-12T10:29:49+00:00Added an answer on June 12, 2026 at 10:29 am

    Why? Readability

    Many things are done in code because of the mantra of “Readability”. It is invaluable in writing code. Your code should be easily inherited by another developer.

    You forgot one

    Or zero, as it were.

  2. Example 0
  3. var x = function(){ }();
    

    And here is why people don’t use that: it is hard to read. Usually these closures become very large, very quick. Someone shouldn’t have to get to the very end of the closure to determine that it is just a variable named x with no self execution – such as var x = function(){}; To this end, developers wrapped their self executing closures with parenthesis: var selfExecuting = (function(){})();, or even plainly (function(){})(); so that from line 1 it was obvious that this was self executing. It also means that if you come across a function like this: var notExecuting = function(){}; you can generally assume at line 1 that it is not self executing.

    Readability is very important when coding. It is important for maintenance. It is important for consistency. It is important for documentation.

    Although, keep in mind, development code should not be production code.

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

Sidebar

Related Questions

Could someone please clarify for me the semantic difference between these two: self.foo =
Is there a semantic difference between these two declaration or is it only syntactic
Is there any semantic difference between writing assertThat(object1, is(equalTo(object2))); and writing assertThat(object1, equalTo(object2))); ?
What is the difference between these two innerclass declarations? Also comment on advantages/disadvantages? case
I don't get the actual (semantic) difference between the two expressions. It is said
What is the semantic difference between these 3 ways of using ivars and properties
What is the semantic difference between the following two methods: public static bool IsNullOrEmpty(this
In Java, is there a semantic difference between using Illegal (as in IllegalArgumentException )
In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and
What is the difference between Semantic-markup and Semantic Web ? Are both same concepts?

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.