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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:43:47+00:00 2026-06-14T13:43:47+00:00

I’m learning javascript from Crockford’s book Javascript: The Good Parts. He explains there how

  • 0

I’m learning javascript from Crockford’s book Javascript: The Good Parts. He explains there how to use closure to encapsulate information. E.g.

var myObject = (function () {
    var value = 0;
    return {
        getValue: function () {
            return value;
        }
    };
}());

If I have understood this correctly, the idea is that we define a function returning an object literal. This function is then invoked to initialize myObject. But what is the point of the outmost brackets?[(f…);]. Isnt the pair () enough to invoke the function we have defined?

  • 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-14T13:43:48+00:00Added an answer on June 14, 2026 at 1:43 pm

    You are correct; the outermost parentheses do nothing, although it is common practice to put them anyway — it helps with readability. When people see a function enclosed in parentheses like that, they expect it to be immediately invoked, whereas if the parentheses were not there it may not be as apparent at first. It’s nothing more than a visual cue here.

    However, note that parentheses around a function are needed in some cases to make it a function expression rather than a function declaration.

    For example, the following will not work:

    function() {
        // Do something...
    }();
    // Throws a syntax error.
    

    In this case, you’ll get a syntax error because the parser is expecting a function declaration, and you didn’t give this function a name. In addition, you can’t immediately invoke a function declaration. You need parentheses to convert this into a function expression:

    (function() {
        // Do something...
    })();
    

    The difference in a function declaration and a function expression is a function declaration can be used to declare a function in the same way as var can be used to declare a variable:

    // Declare the variable foo.
    var foo;
    
    // Declare the function bar.
    function bar() {
        // Do something...
    }
    

    A function expression, however, doesn’t declare a variable. Rather it just creates a function which you can use for something, such as assign it to a variable or just to invoke.

    In order to make a function expression you just have to start the line with something other than function. A common technique, as seen above, is to start it with a parenthesis. As you mentioned you can also start the line with var something = ...:

    var foo = function() {
        return 5;
    }();
    // foo is now 5
    

    You can really start it with anything which would make a valid expression though:

    !function() {
        console.log(1);
    }();
    // logs 1
    
    ~function() {
        console.log(2);
    }();
    // logs 2
    
    +function() {
        console.log(3);
    }();
    // logs 3
    
    [ function() {
        console.log(4);
    }() ];
    // logs 4
    
    'Hello, World' == function() {
        console.log(5);
    }();
    // logs 5
    

    Of course, if you’re trying to store the result of the function in a variable, you won’t be able to use any of these patterns, as they all mess with the return value. Most people stick to wrapping functions in parentheses, whether they are necessary or not, just for the sake of consistency and readability.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
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 am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported

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.