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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:20:34+00:00 2026-06-01T12:20:34+00:00

This is going to be a quick discussion, but I just wanted some feedback

  • 0

This is going to be a quick discussion, but I just wanted some feedback on a revelation I had this morning. Knowing that this…

var addTwoNumbers = function(intOne, intTwo) {
    if ((typeof intOne == 'number') && (typeof intTwo == 'number')) {
        document.write(intOne + intTwo);
    } else {
        document.write('Unable to perform operation.');
    }
};

addTwoNumbers(3, 4);

… behaves essentially the same as this…

(function(intOne, intTwo) {
    if ((typeof intOne == 'number') && (typeof intTwo == 'number')) {
        document.write(intOne + intTwo);
    } else {
        document.write('Unable to perform operation.');
    }
})(3, 4);

… is that to say that the first set of parentheses in the self-invoking function is a “tool” to bypass or work around function execution by reference? In effect, () is the name of the method without actually being the name of the method? Also, because the function is being declared directly at execution, is it faster than the technique using a variable name reference? Just wondrin’.

  • 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-01T12:20:35+00:00Added an answer on June 1, 2026 at 12:20 pm

    OK here’s the reason that “self-invoking functions” (which really aren’t “self-invoking”; there’s an explicit invocation and it’s outside the function proper) are written (by convention) as a parenthesized subexpression.

    JavaScript has two constructs involving the keyword function:

    1. The function declaration statement, which defines a function object and binds it to a name in the local scope (as well as in the function’s local scope but let’s ignore that for now):

      function foo() { /* code */ }
      
    2. The function instantiation sub-expression, which creates a function object in the context of an expression:

      var f = function() { /* code */ };
      

    The problem comes about when you want the second thing, and you want it at the beginning of an expression statement. When the statement starts with the keyword function, the parser assumes that you’re doing 1 above, not 2. Thus, by introducing parentheses around the function instantiation — and remember that parenthesizing a sub-expression is always allowed and does not affect its value in any way — makes the use of the function keyword be interpreted as 2 above.

    There are other ways of forcing the parser to see the statement as an expression too:

    • !function() { /* code */ }();
    • 0, function() { /* code */ }();
    • +function() { /* code */ }();

    are just some examples.

    As to performance, it’s a non-issue here. One thing I will note is that this way of binding an identifier to a function object:

    var name = function() { /* code */ };
    

    is really no better than:

    function name() { /* code */ }
    

    and in some ways it’s worse. In particular, using a function declaration statement gives a name that will show up in stack traces, while defining the function with var does not.

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

Sidebar

Related Questions

Super quick question, my initial thought was this is not going to work, but
Just a quick question, can someone tell me where this is going wrong, I'm
This is going to sound like a silly question, but I'm still learning C,
This is going to be the noobist of all noob questions, but what exactly
This is going to be hard to explain but I'll try my best. I
This is going to sound too silly / too basic - sorry about that,
This is going to be a long post and just for fun, so if
I'm guessing this is going to involve regexp or something, but I'll give it
Just a quick question: I'm working on revamping a script that serves vehicle inventory
this is just a quick probe to see if this is technically possible. I'm

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.