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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:27:21+00:00 2026-05-18T07:27:21+00:00

I just read Ben Alman ‘s article on Immediately-Invoked Function Expressions and wondered about

  • 0

I just read Ben Alman‘s article on Immediately-Invoked Function Expressions and wondered about this part, where he’s introducing function expressions and closures (not really related to IIFEs yet):

// ...doesn't it stand to reason that the function expression itself can
// be invoked, just by putting () after it?

function(){ /* code goes here */ }();

// This works! Well, almost. A minor JavaScript syntax issue actually
// requires that ambiguity between function declarations and function
// expressions be eliminated, which can be done by wrapping the function
// expression in parens.

// The following pattern is used universally to create an anonymous
// closure with "privacy":

(function(){ /* code goes here */ })();

// This parens syntax is also valid (I prefer the previous version):

(function(){ /* code goes here */ }());

That last part struck me. Can anybody explain why there are two different syntactical versions for invoking function expressions?

Was this syntax consciously introduced just for invoking anonymous closures? Or is it a by-product of some other syntactical property?

Why does the second version work anyway? The first one makes sense to me from a parser standpoint. The first pair of parens evaluates to a function object, the second pair invokes this function object. But the second one? It doesn’t look like it’s resolving the mentioned syntactical ambiguity.

Can anybody tell me what I’m missing here?

  • 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-18T07:27:22+00:00Added an answer on May 18, 2026 at 7:27 am

    All of it happens because JavaScript has two parsing contexts : expression and statement. Writing function foo() {} at statement level defines function foo in that scope, whereas function foo() {} at expression level evaluates to a new anonymous function (that can call itself recursively as foo).

    So, whenever that construct is encountered, the parser must determine whether it is at expression or statement level. By default, it assumes to be at statement level, unless convinced otherwise. The brackets are a syntactic way of saying “this is an expression“.

    So, writing (function() {}) causes the parser to treat the function as an expression. Since expressions can only contain other expressions (no statements), it also follows that if you write (something-that-contains function(){} and-other-stuff) then the function in there will still be treated as an expression.

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

Sidebar

Related Questions

I just read this post about why new-line warnings exist, but to be honest
I just read about HTML 5's WebSocket interface. How can I start trying this?
I just read about Statement Expressions Extension in GCC, and I found some unexpected
I just read in an article about JIT Compilation that Java has two modes
I just read this post about adding buttons with a loop programmatically . I
I just read this blog post about a recipe to lazily initialize an object
I just read this article on how to create global hotkeys using Carbon events.
I just read this article from Google. I always thought that closing tags is
I just read this article and it did confuse me a lot. Secondly, this
I just read this topic about the Magento payment flow. It handles about 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.