Computer Science

Javascript For Loop

A JavaScript for loop is a control flow statement that allows you to repeatedly execute a block of code. It consists of three parts: initialization, condition, and iteration, which are used to control the number of times the loop runs. This looping structure is commonly used for iterating over arrays, performing calculations, and executing code based on specific conditions.

Written by Perlego with AI-assistance

3 Key excerpts on "Javascript For Loop"

  • Beginning HTML and CSS
    • Rob Larsen(Author)
    • 2013(Publication Date)
    • Wrox
      (Publisher)
    ch10_eg09.js ):
    for ( var i=0; i<11; i++ ) { document.getElementById( "numbers" ).innerHTML += "<li>" + i + " x 3 = " + (i * 3) +"</li>"; }
    Now look at the for statement in small chunks:
    • var i=0 The counter is assigned to have a value of 0.
    • i<11 The loop should run if the value of the counter is less than 11.
    • i++ The counter is incremented by 1 every time the loop runs.
    The assignment of the counter variable, the condition, and the incrementing of the counter all appear in the parentheses after the keyword for .
    You can also assign several variables at once in the part corresponding to the letter a if you separate them with a comma, for example, var i = 0, j = 5; . It is also worth noting that you can count downward, as well as upward, with loops.

    Infinite Loops and the break Statement

    If you have an expression that always evaluates to true in any loop, you end up with something known as an infinite loop . These can tie up system resources and can even crash the computer; although, some browsers try to detect infinite loops and then stop the loop.
    You can, however, add a break statement to stop an infinite loop; here it is set to 100 (ch10_eg10.js ):
    for ( var i=0; /* no condition here */ ; i++ ) { document.getElementById( "numbers" ).innerHTML += "<li>" + i + " x 3 = " + (i * 3) +"</li>"; if (i == 100) { break; } }
    When the script gets to a break statement, it simply stops running. This effectively prevents a loop from running too many times.

    Events

    All browsers are expected to support a set of events known as intrinsic events such as the onload event, which happens when a page has finished loading, onclick for when a user clicks on an element, and onsubmit
  • JavaScript For Kids For Dummies
    • Chris Minnick, Eva Holland(Authors)
    • 2015(Publication Date)
    • For Dummies
      (Publisher)
    Part VI

    Loops

    In this part …
    What’s This Loop For?
    Using While Loops
    Building a Lemonade Stand
    For information on advanced looping with JavaScript, go to www.dummies.com/extras/javascriptforkids .
    Passage contains an image Chapter 17

    What’s This Loop For?

    for loops are useful
    for when you know in advance how many times you need to do something. You can use a for loop to count to 10, or to count to 1,000,000. It’s all the same to JavaScript!
    In this chapter, we look at one of the most popular types of loops in JavaScript: the for loop. We use for loops to create our own weather forecasting app.

    Introducing the for Loop

    The for loop is the most commonly used type of loop in JavaScript. Here’s a sample for loop that prints out the words Hello, JavaScript! 500 times to the JavaScript console.
    for (var counter = 0; counter < 500; counter++) {     console.log(counter + ": Hello, JavaScript!"); }
    Figure 17-1 shows what this code looks like when it’s run in the JavaScript console.
    Figure 17-1: Saying “Hello, JavaScript!” 500 times.
    This isn’t the most exciting use for a loop, but you can certainly see that it’s easier to use a loop than it would be to type out 500 console.log statements!
    Let’s take a closer look at how to write for loops.

    The three parts of the for loop

    The for loop is made up of three different statements:
    • Initialization: The initialization statement declares a variable that the loop will use to keep track of how long it has been looping.
    • Condition: A Boolean expression to be evaluated with each iteration of the loop.
    • Final expression: An expression to be evaluated after each loop iteration.
    Here’s how our Hello, JavaScript loop works:
    1. A new variable — in this case, counter — is initiated with the value of 0.
    2. A test is done to check whether counter is less than 500.
      If it is, then the statements inside the loop are run. In this case, the console.log statement will print out Hello, JavaScript!
  • HTML5 and CSS3 All-in-One For Dummies
    • Andy Harris(Author)
    • 2014(Publication Date)
    • For Dummies
      (Publisher)
    endless loops are especially troubling in JavaScript because they can crash the entire browser. If a JavaScript program gets into an endless loop, often the only solution is to use the operating system task manager (Ctrl+Alt+Delete on Windows) to shut down the entire browser.
    The easy way to make sure your loop works is to remember that while loops need all the same features as for loops. (These ideas are built into the structure of a for loop. You're responsible for them yourself in a while loop.) If your loop doesn't work, check that you've followed these steps:
    • Identify a key variable: A while loop is normally based on a condition, which is usually a comparison (although it might also be a variable or function that returns a Boolean value). In a for loop, the key variable is almost always an integer. While loops can be based on any type of variable.
    • Initialize the variable before the loop: Before the loop begins, set up the initial value of the key variable to ensure the loop happens at least once. (How does the variable start?)
    • Identify the condition for the loop: A while loop is based on a condition. Define the condition so the loop continues while the condition is true , and exits when the condition is evaluated to false . (How does the variable end?)
    • Change the condition inside the loop: Somewhere inside the loop code, you need to have statements that will eventually make the condition false . If you forget this part, your loop will never end. (How does the variable change?)
    This example is a good example of a while loop, but a terrible way to handle security. The password is shown in the clear, and anybody could view the source code to see the correct password. There are far better ways to handle security, but this is the cleanest example of a while
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.