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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:16:26+00:00 2026-06-14T08:16:26+00:00

Was trying to figure out how to properly namespace my JavaScript in order to

  • 0

Was trying to figure out how to properly namespace my JavaScript in order to avoid conflicts with other libraries as well as global items. I was reviewing some code and noticed something I just do not understand when it comes to JavaScript. In all reality I don’t even know how to ask the question so I will use some contrived code examples.

var varFuncTest = (function(){
         _funcTest = function(params){
       return "These are the params that were passed in: " + params;
     };
     return _funcTest;
})();

console.log(varFuncTest("params params params"));

OUTPUT: These are the params that were passed in: params params params

When I run this code it works as expected but that is only due to the fact that when I declared the variable I wrapped the function which is building and returning a specialized function in () and then added () right afterwards. When I log the function call I get the correct string printout.

Now when I remove that extra set of () from the end, because they looked unneeded to my untrained eyes, the log prints out the object type instead of the string.

var varFuncTest = (function(){
         _funcTest = function(params){
           return "These are the params that were passed in: " + params;
         };
         return _funcTest;
    });

    console.log(varFuncTest("params params params"));

OUTPUT: function()

What is going on here? Why is the second set of () needed? If this is a special function of the JavaScript language, what is it referred to and where can its documentation be found within the JavaScript API?

The code is identical minus the ending () on the 6th line of the first example. It is not a typo as it actually affects the execution within JavaScript. They both return a function object which is being assigned to the variable.

  • 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-14T08:16:27+00:00Added an answer on June 14, 2026 at 8:16 am

    Javacript has the concept of an immediately invoked function. This is a function that is called as soon as it is defined. These need to be function expressions instead of function statements.

    var a = function() {return "hi"}; // A reference to a function.. to call later.
    alert(a()); // Call the function now.
    

    vs

    var a = (function() {return "hi"}()); // Call the function, return the value in one stop.
    // a now has the result of the function.
    alert(a);
    

    Many of the answers given are true, or true-ish, but there is another aspect that none of them have mentioned.

    When doing an assignment, such as

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

    The outer-most level of parens are NOT needed by the compiler, but they server to inform the human reading the code that a immediately-invoked function is in place.

    On the other hand, code like

    (function() {}());  // One style
    (function() {})();  // Another style
    

    the parens are needed to force the compiler to read an expression, and thus a function-expression, rather than a function statement.

    Using almost any other operator in front of the function keyword works, such as:

    !function() {}();  // Another style
    

    While that works, it isn’t the normal ‘idiom’ to-to-say of Javascript.

    TL-DR: The parens are there more the sake of humans than computers.

    BTW: Please stick with the standard coding conventions. There are a lot of non-standard-but-legal things you can do. Don’t. It confuses the person who comes after you.

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

Sidebar

Related Questions

I'm trying to figure out how to structure data properly in PHP in order
I'm trying to figure out how to properly serialize my XmlDocument and send it
Trying to figure out how I can do this properly. The print_r looks like
Trying to figure out how to identify unique users properly with cookies and perhaps
I'm trying to figure out if there is a css property (or any other
I'm trying to figure out whats the best way to get checkboxes to properly
Hey, I'm trying to figure out how to display the ▼ character properly in
I am trying to figure out how to properly setup my crossdomain file. Here
I'm trying to figure out a way to use the Reflection.Emit namespace to remove
I'm trying to figure out how to properly store foreign keys when the key

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.