JavaScript Finder

Find what your looking for.

  • You are here: 
  • Javascript While Loop

Welcome to JavaScript Finder

Javascript While Loop
  • A while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement. The while construct consists of a block of code and a condition. The condition is first evaluated - if the condition is true the code within the block is then executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop.

    JavaScript : While Loop
    <script language="JavaScript">
    <!--
    var i=0;
    while (i<=10)
    {
    document.write("The number is " + i);
    document.write("<br />");
    i=i+1;
    }
    -->
    </script>


  • Here i=0 at the start of the script each time it loops i=i+1. While i<=10 it will repeat the loop until the condition becomes false. False would mean that i>10.

    The output would be.
    The number is 0
    The number is 1
    The number is 2
    The number is 3
    The number is 4
    The number is 5
    The number is 6
    The number is 7
    The number is 8
    The number is 9
    The number is 10



Javascript While Loop

More Help