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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:20:24+00:00 2026-06-05T22:20:24+00:00

I am at a loss how best to approach for loops in JavaScript. Hopefully

  • 0

I am at a loss how best to approach for loops in JavaScript. Hopefully an understanding of for loops will help shed light on the other types of loops.

Sample code

for (var i=0; i < 10; i=i+1) {
    document.write("This is number " + i);
}

My understanding is that when i has been initialized, it starts with the value of 0 which then evaluated against the condition < 10. If it is less than 10, it the executes the statement document.write("This is number + i); Once it has executed the preceding statement, only then does it increment the next value by 1.

Guides I have consulted:

  1. http://www.functionx.com/javascript/Lesson11.htm
  2. http://www.cs.brown.edu/courses/bridge/1998/res/javascript/javascript-tutorial.html#10.1
  3. http://www.tizag.com/javascriptT/javascriptfor.php

Now the guide at http://www.functionx.com/javascript/Lesson11.htm seems to indicate otherwise i.e.

To execute this loop, the Start condition is checked. This is usually
the initial value where the counting should start. Next, the Condition
is tested; this test determines whether the loop should continue. If
the test renders a true result, then the Expression is used to modify
the loop and the Statement is executed. After the Statement has been
executed, the loop restarts.

The line that throws me is "If the test renders a true result, then the Expression is used to modify the loop and the Statement is executed". It seems to imply that because 0 is less than 10, increment expression is modified which would be 0 + 1 and THEN the statement, e.g. document.write is executed.

My problem

What is the correct way to interpret for loops? Is my own comprehension correct? Is the same comprehension applicable to other programming languages e.g. PHP, Perl, Python, etc?

  • 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-05T22:20:25+00:00Added an answer on June 5, 2026 at 10:20 pm

    Your quoted source is wrong, and we can prove it…

    The basis of the for loop has four separate blocks which may be executed:

    for(initialise; condition; finishediteration) { iteration }

    Fortunately we can execute a function in each of these blocks. Therefore we can create four functions which log to the console when they execute like so:

    var initialise = function () { console.log("initialising"); i=0; }
    var condition = function () { console.log("conditioning"); return i<5; }
    var finishediteration = function () { console.log("finished an iteration"); i++; }
    var doingiteration = function () { console.log("doing iteration when `i` is equal", i); }
    

    Then we can run the following, which places the above functions into each block:

    for (initialise(); condition(); finishediteration()) {
        doingiteration();
    }
    

    Kaboom. Works.

    If you viewing this page using Safari on the Mac then you can AppleAlt + I and copy the above two snippets, in order, into the console and see the result.

    EDIT, extra info….

    Also… the finished iteration block is optional. For example:

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

    does work.

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

Sidebar

Related Questions

I am at a complete loss because my application icon simply will not register.
Not much code since I'm a bit at a loss on how to start.
I am at a loss here of a best-practice to pass XML back and
What is the best way to rebuild a model without loss data in MySQL
What are the best steps to take to prevent bugs and/or data loss in
I'm at a loss. Right now my site will redirect to a logon page
I have a question about best practices regarding how one should approach storing complex
I'm at a loss for where to get the best information about the meaning,
I am trying to figure out the best approach to parsing large datasets of
I'm at a loss, I can't see the reason something as simple as this

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.