Www.JavaBasics.We.Bs

home HOME

JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Variables
JS Operators
JS Functions
JS Conditional
JS Looping
JS Guidelines

JS References
JS Array
JS Boolean
JS Date
JS Math
JS String
JS HTML DOM

Examples/Quiz
JS Basic Examples
JS Core Examples
JS DOM Examples
JS Quiz


JavaScript Looping

previous next


Looping statements in JavaScript are used to execute the same block of code a specified number of times.


Examples

For loop
How to write a For loop. Use a For loop to run the same block of code a specified number of times

Looping through HTML headers
How to use the For loop to write the HTML headers.

While loop
How to write a While loop. Use a While loop to run the same block of code while or until a condition is true

Do while loop
How to write a Do While loop. Use a Do While loop to run the same block of code while or until a condition is true. This loop will always be executed once, even if the condition is false, because the statements are executed before the condition is tested


Looping

Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to do this.

In JavaScript we have the following looping statements:

  • while - loops through a block of code while a condition is true
  • do...while - loops through a block of code once, and then repeats the loop while a condition is true
  • for - run statements a specified number of times

while

The while statement will execute a block of code while a condition is true.. 

while (condition)
{
    code to be executed
}



do...while

The do...while statement will execute a block of code once, and then it will repeat the loop while a condition is true

do
{
    code to be executed
}
while (condition)



for

The for statement will execute a block of code a specified number of times

for (initialization; condition; increment)
{
    code to be executed
}



previous next





Copyright © 2007-2008, Www.JavaBasics.We.Bs All rights reserved.