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

  • Home
  • SEARCH
  • 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 9294265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:19:53+00:00 2026-06-18T21:19:53+00:00

I want to call some javascript code that for its own purposes needs to

  • 0

I want to call some javascript code that for its own purposes needs to define some variables, but I want to isolate this so it doesn’t pollute the global scope. So I wrap everything in an anonymous function object, call it immediately, and don’t assign it or the result to anything:

function() {
    var myInnerHelperFunction = function(object) {
        //do some work
    };

    var anObject = ...;

    myInnerHelperFunction(anObject);
}();

But I get a syntax error: SyntaxError: Unexpected token (

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

    You can’t invoke a function declaration. You need to make it an expression instead. The most commonly seen technique to achieve this is to wrap the function in parentheses:

    (function() {
        var myInnerHelperFunction = function(object) {
            //do some work
        };
    
        var anObject = ...;
    
        myInnerHelperFunction(anObject);
    }());
    

    However, note that any unary operator will also work (anything that causes the construct to be parsed as an expression, rather than a declaration):

    ~function example() {
        console.log("example");
    }();
    

    To expand upon this further, your code actually throws a syntax error because your function is missing an identifier. A “program” can only contain statements and function declarations. Therefore, your code must be parsed as a function declaration. A function declaration must have an identifier. Yours doesn’t, so your program is invalid.

    When you wrap parentheses around the function, the parse tree is different. The parentheses get parsed as an “expression statement”, which can contain a expression. Since functions can be parsed as either expressions or declarations, depending on the context in which they appear, this is legal, and the function is parsed as an expression. Since function expressions can be immediately invoked, this works.

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

Sidebar

Related Questions

I want to call subroutines in Perl like: sub temp { ---- some code
Actually I have some MVC3 Applications and I want to call this applications from
I want to create some kind of AJAX script or call that continuously will
I have some JavaScript/jQuery code that watches for the changed event on a checkbox:
Alright... I'm trying to write some javascript code that will apply to an html
I'm struggling with trying to modify some Javascript code. I know what I want
I have some JavaScript code that creates some div elements and it sets their
I want to call a url with passing some parameter and i want to
I'm using CoreLocation,and I want to call a method to do some computations on
I'm getting some very wierd errors. The compiler seems to want to call the

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.