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

Ask A Question

Stats

  • Questions 454k
  • Answers 454k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There aren't any up-to-date books that I know of. You… May 15, 2026 at 9:45 pm
  • Editorial Team
    Editorial Team added an answer Instead of the mouseover and mouseout events, if the elements… May 15, 2026 at 9:45 pm
  • Editorial Team
    Editorial Team added an answer Ok so this one works. Just in case anybody wants… May 15, 2026 at 9:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.