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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:42:20+00:00 2026-05-15T15:42:20+00:00

Example: var o = {}; for(var i = 0; i < 5; i++) {

  • 0

Example:

var o = {};
for(var i = 0; i < 5; i++) {
  o[i] = function () {
      console.log(i);
  };
}

o[3]();

When I call o3, it will always display 5 on the console, even if I call o0, o4, or any one of those. It will always display 5 because that’s the last value i had. How do I make it display the value of i when the anonymous function is made? As in o3 should display 3 in the console.

  • 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-15T15:42:20+00:00Added an answer on May 15, 2026 at 3:42 pm

    You should do:

    var o = {};
    for(var i = 0; i < 5; i++) {
      (function(i) { // <- self-executing anonymus function with a parameter i
        o[i] = function () {
            console.log(i); // <- i here will be the argument i, 
                            //    not the i from the loop
        };
      })(i); // <- pass i as an argument
    }
    
    o[3]();
    

    What happens is that you create something called closure so as to keep the state of i.

    Closure means that an inner function keeps a reference to the outer function and so gains access to its variables and parameters (even after the outer function has returned).

    A trivial example for closure is:

    function outer(arg) { 
      var variable = 5;
      arg = 2;
      function inner() {
        alert(variable);  // <- the inner function has access to the variables
        alert(arg);       //     and parameters of the outer function
      }
    }
    

    A self-executing (self-invoking, or immediate) function is a function that is called right after it is declared.

    (function() {
      alert("executed immediately");
    })();
    

    The combination of both and the fact that only functions have scope in Javascript, leads you to the technique mentioned above to create a scope with a new function which keeps the state of i, which would otherwise be changed by the inner function because of closure.

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

Sidebar

Related Questions

I want to create a quick function that will console.log a variable name and
I have a custom logging function to log to the firebug console that looks
For example: (function() { var proxied = window.eval; window.eval = function() { return proxied.apply(this,
How can I bind to the right of the function? Example: var square =
I have seen this example on the web: $('#questionTextArea').each( function() { var $this =
Ok, in my code I have for example, this: $('.follow').click(function(){ var myself = $(this);
For example suppose client side I have a JavaScript function function getLocation() { var
We have the following example in node.js var http = require('http'); http.createServer(function(request, response) {
For example suppose I have the following app.get('/', function(req, res) { var ip; if(req.headers['x-forwarded-for']){
one question I always ask myself is how is it possible that javascript has

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.