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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:17:46+00:00 2026-05-21T20:17:46+00:00

I read the () at the end of the closure will execute it immediately.

  • 0

I read the () at the end of the closure will execute it immediately. So, what is the difference between these two. I saw the first usage in some code.

thanks.

for (var a=selectsomeobj(),i=0,len=a.length;i<len;++i){
        (function(val){
            anotherFn(val);
        })(a[i]);
}

for (var a=selectsomeobj(),i=0,len=a.length;i<len;++i){
            anotherFn(a[i]);
}
  • 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-21T20:17:47+00:00Added an answer on May 21, 2026 at 8:17 pm

    In this example there are no differences. In both cases, anotherFn gets executed immediately.

    However, an immediate function is often used when a function is created in a loop.

    Consider this example (more or less pseudo code):

    for(var i from 1..10) {
        elements[i].onclick = function() {
             alert(values[i]);
        }
    }
    

    As JavaScript has only function scope, no block scope, all the event handlers share the same i, which will have the value 10 after the loop finished. So every handler will try to alert values[10].

    By using an immediate function, a new scope is introduced which “captures” the current value of the loop variable:

    for(var i from 1..10) {
        (function(index) {
            elements[i].onclick = function() {
                 alert(values[index]);
            }
        }(i));
    }
    

    As this is sometimes hard to read, creating a standalone function which returns another function is often better:

    function getHandler(value) {
         return function(){alert(value);};
    }
    
    for(var i from 1..10) {
         elements[i].onclick = getHandler(values[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have some code to read from the start to end of a
If I skip, do I still need to read the end element? reader.ReadStartElement(ENCODING); reader.Skip();
I'm helping maintain a program that's essentially a friendly read-only front-end for a big
Please read my update at the end of question after reading the answers: I'm
I need to read data added to the end of an executable from within
I use read more at the end of paragraph just for reminder for user
I read this article Managing Hierarchical Data in MySQL At the end of this
The specification of ReadableByteChannel.read() shows -1 as result value for end-of-stream. Moreover it specifies
I read file, but in the end of file i get unknown symbols: int
I need to read a file in PHP, but I only know the end

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.