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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:47:16+00:00 2026-05-19T00:47:16+00:00

Possible Duplicate: What does (function($) {})(jQuery); mean? I’ve seen a lot of jQuery code

  • 0

Possible Duplicate:
What does (function($) {})(jQuery); mean?

I’ve seen a lot of jQuery code with the following sort of syntax, but I don’t really understand what it means. It shows up in this answer and this answer on a question about code organization. Both talk about namespacing, so I’m guessing that’s what it accomplishes.

var foo = (function () {
    var someVar;

    function someFunc() {
        return true;
    }
})();

Is this for namespacing, and how does it work? Sometimes there is a name (the namespace?) in the final set of parentheses, sometimes not. What is the difference between the two?

  • 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-19T00:47:16+00:00Added an answer on May 19, 2026 at 12:47 am

    The () that wrap the function turns the anonymous function declaration into a function expression that can then be immediately invoked with the () that follows the expression.

    In this case, the outer () really isn’t necessary since the var foo = would turn it into an expression. Also, the value of foo will be undefined since the function invocation doesn’t return anything.

    It can be used for creating a new variable scope, since a function is the only way to accomplish that in javascript. (Javascript doesn’t have block scope.)

    So the someVar variable is not accessible to the outer scope. There may be times when it is desirable to make it accessible in a controlled manner. To do this, you can pass a function out of that scope which references someVar. Then after the function invocation exits, its execution context will remain intact, and someVar will be available in whatever manner the function you passed out provides.

    This is called creating a closure.

    Let’s say you passed a value into the invocation, and assigned it to someVar. You could then return a function out of the invocation to the foo variable. If that function you return references someVar, then you could use that function to get its value.

    var foo = (function ( str ) {
        var someVar = str;
    /*
        function someFunc() {
            return true;
        }
    */
        return function() {
            alert( someVar );
        };
    })( 'somevalue' );
    
    foo(); // alerts 'somevalue'
    

    As you can see, the function now referenced by foo can still access someVar.

    Let’s say you changed it so that the function returned to foo can accept an argument, which will update the value of myVar.

    var foo = (function ( str ) {
        var someVar = str;
    /*
        function someFunc() {
            return true;
        }
    */
        return function( n ) {
            if( n ) {
                someVar = n;
            } else {
                alert( someVar );
            }
        };
    })( 'somevalue' );
    
    foo(); // alerts 'somevalue'
    
    foo( 'newvalue' ); // give it a new value
    
    foo(); // alerts 'newvalue'
    

    Now you can see, that the function in foo really does access that variable, as it is able to change its value, and reference the new value that it previously set.

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

Sidebar

Related Questions

Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: What does map(&:name) mean in Ruby? I was watching a railscast and
Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: What Ruby IDE do you prefer? I've generally been doing stuff on
Possible Duplicate: How do you send email from a Java app using Gmail? How

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.