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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:29:20+00:00 2026-05-22T14:29:20+00:00

How does JavaScript closure work in this case and to be more specific: what

  • 0

How does JavaScript closure work in this case and to be more specific: what does the (i) at the end do?

for(var i = 0; i < 10; i++) {
    (function(e) {
        setTimeout(function() {
            console.log(e);  
        }, 1000);
    })(i);
}

Also I’m trying to implement it in my code, and it seems I don’t get it right

for (var i=0; i < len; i++) {

    var formID = document.forms["form-" + i];
    $(formID).bind("submit", validate);

    $(formID).bind("change", function(i){
        var divI = '#ind-' + i;
        $(divI).css("background-color","green");
    })(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-22T14:29:20+00:00Added an answer on May 22, 2026 at 2:29 pm

    This is a pattern used to create local scope around a variable. If this wasn’t used then every call to console.log(i) would log the value of i (10) after the for loop finished.

    for(var i = 0; i < 10; i++) {
        // create new function
        (function(e) {
            // log each counter after 1 second.
            setTimeout(function() {
                console.log(e);  
            }, 1000);
        // execute it with the counter
        })(i); 
    }
    

    The above is the same as this.

    function foobar(e) {
        setTimeout(function() {
             console.log(e);
        }, 1000);
    }
    
    for (var i = 0; i < 10; i++) {
        (
            foobar
        )(i);
    }
    

    The real problem here is creating functions in a loop. don’t do it 🙂

    Your code

    for (var i=0; i < len; i++) {
    
        var formID = document.forms["form-" + i];
        $(formID).bind("submit", validate);
        // create a full closure around the block of code
        (function() {
            $(formID).bind("change", function(i){
                var divI = '#ind-' + i;
                $(divI).css("background-color","green");
            })//(i); Don't call (i) here because your just trying to execute the 
            // jQuery element as a function. You can't do this, you need to wrap 
            // an entire function around it.
        })(i);
    }
    

    But that is wrong, you want to delegate this job to something else.

    function makeGreen(form, i) {
        $(form).change(function() {
             $("#ind-"+i).css("background-color", "green");
        });
    }
    
    for (var i=0; i < len; i++) {
        var formID = document.forms["form-" + i];
        $(formID).bind("submit", validate);
        // call a helper function which binds the change handler to the correct i
        makeGreen(formID, i);
    }
    

    If you want to get a bit clever you can get rid of these anonymous functions

    function makeGreen() {
         var divId = $(this).data("div-id");
         $(divId).css("background-color", "green");
    }
    
    for (var i=0; i < len; i++) {
        $(document.forms["form-" + i])
            .bind("submit", validate)
            // store i on the form element
            .data("div-id", "#ind-" + i)
            // use a single event handler that gets the divId out of the form.
            .change(makeGreen);
    }
    

    Edit

    ( // contain the function we create.
        function(parameterA) {
             window.alert(parameterA);
        }
    ) // this now points to a function
    ("alertMessage"); // call it as a function.
    

    Is the same as

    ( // contain the window.alert function
        window.alert
    ) // it now points to a function
    ("alertMessage"); // call it as a function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does Javascript work in the browser address bar? To be more specific: how
Why does this code: function answer(x) { function closure() { var x = x
JavaScript does funky automatic conversions with objects: var o = {toString: function() {return 40;
Does javascript coding work better with any particular server language?
Why does this javascript return 108 instead of 2008? it gets the day and
Why does the JavaScript function encodeURIComponent encode spaces to the hex Unicode value %20
Which algorithm does the JavaScript Array#sort() function use? I understand that it can take
This is a function taken from Pro JavaScript techniques that I'm trying to understand,
Does JavaScript have a built-in function like PHP's addslashes (or addcslashes ) function to
Does Javascript or jQuery have sometime like the in statement in Python? a in

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.