W3Schools

 

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 String Object
JS Array Object
JS Date Object
JS Math Object
JS Window Object
JS Frame Object
JS Form Object
JS Browser Object

Examples/Quiz
JS 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

Product Spotlight


Want To Be A Web Master?

If you want to be a Web Master, you will have to host your web site with an ISP (Internet Service Provider).

HostVentures provides affordable, quality web hosting with a 30-day money back guarantee and 24/7 technical support!

HostVentures will house your site in a top of the line Level3 Communication facility, along with companies such as Yahoo, America Online, and Earthlink. The data center offers the fastest internet connections (OC192) with redundancy in the network equipment, power, cooling and servers.

Visit HostVentures


The Web Design Business Kit

Brendon Sinclair has a unique philosophy that everyone from Freelancers to the huge International Web Design Shops can learn from:

  • Your Web Design and Development skills don't matter!
  • You don't have to be the cheapest bidder to win!
  • Charging by the hour is the worst thing you can do!
  • Your clients are everywhere!
  • When the project finishes, your income should not!

Want to know more...?

Receive 4 FREE CHAPTERS in your inbox:
http://www.sitepoint.com/launch/3a9053/2/4

or read more:
http://www.sitepoint.com/launch/3a9053/3/4

Book

We Help You For Free. You Can Help Us!


Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Google  Yahoo  Alta Vista  MSN  Lycos  Excite  Ask Jeeves  HotBot


W3Schools is for training only. We do not warrant the correctness of the content. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2004 by Refsnes Data. All Rights Reserved.


Validate How W3Schools was converted to XHTML Validate

E Components
Ecommerce
Components
for ASP.NET


WEB HOSTING

$15 Domain Name
Registration
Save $20 / year!



UK Domain Names
Web Hosting
Merkaweb Dominios


Buy UK Domain Names
Register Domain Names


Website Hosting
Domain Hosting
Web Hosting


Cheap Domain Names UK


Domain Names UK
Quality Web Hosting
Webmail & POP3 Email


Web Hosting UK
Dedicated Servers UK
Domain Names UK


Business Web Hosting
Free Web Hosting
Cheap Web Hosting
Web Hosting Reseller


Website Design UK

HOTELS

Amsterdam Hotels
Boston Hotels
Chicago Hotels
London Hotels
Los Angeles Hotels
Miami Hotels
New York Hotels
New Orleans Hotels
Paris Hotels
Rome Hotels
Hotel Reservations


Hotel Directory
Hotel Rooms

Web Building

Website Templates
Website Builder
Flash Templates
Web Templates

Shopping

Garden decor
Online Coupons
Cooking tools
Men & women clothing
Office supply

Software

Student Price Software


Your Own Web Site?

Read W3Schools
Hosting Tutorial


WebDev Books

The Web Design Business Kit Grow Your Web Design Business!

HTML Utopia: Designing Without Tables Using CSS Learn CSS Positioning the easy way!

Build Your Own Database Driven Website Using PHP & MySQL Learn PHP & MySQL the easy way!

SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Security
Web Statistics
Web Standards

Web Quality