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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:41:31+00:00 2026-06-12T19:41:31+00:00

I have the following small code snippet with the following expected and real output.

  • 0

I have the following small code snippet with the following expected and real output. My question is quiet simple. Why is it printing it this sequence? and how to I print the expected output?

Gr,

expected result:

0
1
2
0
1
2

real result:

0
1
2
3
3
3

this is the code:

var functions = [];

for (var i=0; i<10; i++) {
  console.log (i);
  functions.push (function () {
    console.log (i);
  });
};

for (var j=0; j<functions.length; j++) {
  functions[j] ();
};
  • 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-06-12T19:41:33+00:00Added an answer on June 12, 2026 at 7:41 pm

    The functions that you push into the array doesn’t log the value of i as it was when the function was created, they log the value of i at the time that the function is called.

    Once the first loop ends, the value of i is 10, therefore any of the functions called after that will log the value 10.

    If you want to preserve the value of i at different states, you can use a closure to make a copy of the value:

    for (var i=0; i<10; i++) {
      console.log (i);
    
      (function(){
        var copy = i;
    
        functions.push (function () {
          console.log (copy);
        });
    
      })();
    
    };
    

    The local variable copy will get the value of i and retain the value. You can also pass the value as a parameter to the function:

    for (var i=0; i<10; i++) {
      console.log (i);
    
      (function(copy){
    
        functions.push (function () {
          console.log (copy);
        });
    
      })(i);
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Work on this small test application to learn threading/locking. I have the following code,
I have a small question about threading in Windows. I have the following code
I have the following code snippet of a small Thread class I am trying
I have a small snippet of code. When I run this on my DevC++
I'm doing a small app in Excel, and I have the following code when
I have the following parser grammar (this is a small sample): expr: ident assignop
A small - little question. I have seen many application having buttons like following.
I have a small doubt I have following code bool res= false; if(cond1) {
I have the following code on my model: has_attached_file :photo, :styles => { :small
I have the following html code: <i class=small ele class1></i> <i class=medium ele class1></i>

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.