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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:07:56+00:00 2026-05-16T16:07:56+00:00

Possible Duplicate: Javascript closure inside loops – simple practical example Rather than explaining the

  • 0

Possible Duplicate:
Javascript closure inside loops – simple practical example

Rather than explaining the question, I’ll give an example:

for (var i = 0; i < 100; i ++) {
  get_node(i).onclick = function() {
    do_something_very_important(i);
  }
}

Is there any way to have the value of i substituted into the function upon creation rather than execution? Thanks.

  • 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-16T16:07:56+00:00Added an answer on May 16, 2026 at 4:07 pm

    Yes, you can, but that won’t work for the example you provided. You would be having a very common closure problem in that for loop.

    Variables enclosed in a closure share the same single environment, so by the time the onclick callback is called, the for loop will have run its course, and the i variable will be left pointing to the last value it was assigned. In your example, the do_something_very_important() function will be passed the value 100 for each node, which is not what you intend.

    You can solve this problem with even more closures, using a function factory:

    function makeClickHandler(i) {  
      return function() {  
        do_something_very_important(i);
      };  
    }
    
    // ...
    
    for(var i = 0; i < 100; i++) {
      get_node(i).onclick = makeClickHandler(i);
    }
    

    This can be quite a tricky topic, if you are not familiar with how closures work. You may want to check out the following Mozilla article for a brief introduction:

    • Mozilla Dev Center: Working with Closures

    UPDATE:

    You could also inline the above function factory as @adamse suggested in the other answer. This is actually a more common approach, but is practically the same as the above:

    for(var i = 0; i < 100; i++) {
      get_node(i).onclick = (function(p) {
        return function () {
          // we could have used i as a parameter variable as well,
          // but we're using p to better illustrate what's happening
          do_something_very_important(p); 
        }
      })(i);
    }
    

    Any yet another solution is to enclose each iteration in its own scope, by using self invoking anonymous functions:

    for(var i = 0; i < 100; i++) {
      (function (p) {
        // we now have a separate closure environment for each
        // iteration of the loop
        get_node(i).onclick = function() {
          do_something_very_important(p);
        }
      })(i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Javascript closure inside loops - simple practical example How can I make
Possible Duplicate: Javascript closure inside loops - simple practical example Calling setTimeout function within
Possible Duplicate: Javascript closure inside loops - simple practical example I am using webservice
Possible Duplicate: Javascript closure inside loops - simple practical example Javascript: closure of loop?
Possible Duplicate: Javascript closure inside loops - simple practical example Seen many posts talking
Possible Duplicates: Javascript closure inside loops - simple practical example Javascript closure “stores” value
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I have
Possible Duplicate: What is the 'new' keyword in JavaScript? creating objects from JS closure:
Possible Duplicate: JavaScript Variable inside string without concatenation - like PHP In PHP, double
Possible Duplicate: javascript substring How can I do substr (php) in JS? Example code

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.