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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:15:31+00:00 2026-05-15T18:15:31+00:00

I have couple of questions about the javascript for loop. First question: for (i=0;

  • 0

I have couple of questions about the javascript for loop.

First question:

for (i=0; i<=2; i++) {;}
console.log(i);

Output is 3. Shouldn’t it be 2?

Second question:

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

Timeouts are set correctly: 0, 1000 and 2000. But the output is 3,3,3 (should be 0, 1, 2). Does this mean the delayed functions are executed after the loop exits? Why?

What should I read to understand all this mysterious javascript stuff?

Thank you.

  • 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-15T18:15:31+00:00Added an answer on May 15, 2026 at 6:15 pm

    First question:

    No because the i variable is incremented after the last successful iteration, then the condition is checked and it evaluates to false so the loop ends.

    The for statement is composed by:

    for ([initialExpression]; [condition]; [incrementExpression])
      statement
    

    And it is executed in the following steps:

    1. The initialExpression is evaluated at the start
    2. The condition is evaluated, if it evaluates to false, the loop ends, if it evaluates to true, the statement is evaluated.
    3. Evaluate the statement.
    4. The incrementExpression is evaluated, go to step 2.

    Second question:

    The function is executed asynchronously after the loop has ended, at this time as you know i contains 3.

    The common workaround for this is to use a function to preserve the value of the looping variable on each iteration, for example:

    for (var i=0; i<=2; i++) {
      (function (i) {
        setTimeout(function(){console.log(i);}, i*1000);
      })(i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of questions about good design with EF4 in an ASP.NET
I have quick question about jQuery plugins, hope somebody out there has some advice.
I have an HTML file with a couple of external JavaScript files. Here's an
I have a couple questions regarding using the entity manager in a JavaSE environment.
I've noticed that there are a couple of similar questions and answers at SO
A couple of years ago, before I knew about Stack Overflow, I was working
I'm working with an array of JavaScript Objects as such: var IssuesArray = [{"ID"
I'm kind of new to threading in C# and had a couple of questions
This isn't a programming question but it's about the SDKs and the IDE. I've
Hopefully, this will be an easy answer for someone with Javascript time behind them...

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.