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 8020349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:40:17+00:00 2026-06-04T21:40:17+00:00

I’ve seen these different patterns when calling a JavaScript function expression: Pattern #1 var

  • 0

I’ve seen these different patterns when calling a JavaScript function expression:

Pattern #1

var global = function () {
    return this;
}();

Pattern #2

var global = (function () {
   return this;
}());

Pattern #3

var global = (function () {
    return this;
})();

In my testings all seem to work (at least with most current browsers). To my “taste”, pattern #3 is better, since it’s more explicit than #1, and it returns the function before actually calling it, as opposed to #2.

But I was wandering if there is a preferred or more correct one…

  • 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-04T21:40:19+00:00Added an answer on June 4, 2026 at 9:40 pm

    So, the initial problem is that the code below:

    function () {
    
    }();
    

    Raises an exception. That’s because the JS parser is not able to automatically determined if it’s a function declaration or a function expression. All the patterns you described are used to explicitly tell to JS “hey dude, that’s an expression”. But in you specific case you don’t have that problem, because if you have a variable in front, JS is perfectly able to understand that is an expression, not a declaration,and it doesn’t raise any syntax error.

    In that specific case there is no need to any “hack”, you can simply have:

    var _global = function () {
    
    }();
    

    And that’s it.
    The question now is: which “hack” use when you need to have a immediate function invocation without any variable? All the pattern you described are okay, Crockford tends to prefer this one:

    (function(){
    
    }());
    

    That’s makes sense to me, at least because you have a full isolated block, compared to:

    (function(){
    
    })();
    

    However, if you join your code with other scripts, or you have previous code, you could end up with some problem if they don’t use semicolon at the end:

    a = b + c
    (function() {
    
    }());
    

    In this scenario JS consider c a function’s call:

    a = b + c(function() { }());
    

    To avoid that, developers usually add a semicolon in front, just to be safe:

    ;(function() {
    
    }());
    

    I personally prefer some operator instead, like the bang (!):

    !function () {
    
    }();
    

    With that you don’t have the issues you have using parenthesis. Less characters, and gives to me a sense of “execution”. But, like the other approach, it’s an hack and it’s a matter of personal taste. For instance Crockford dislike A LOT that bang stuff.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.