Three JS loop keyword examples (for and while) _ Basics

Source: Internet
Author: User

Three ways of writing a loop:

<!doctype html>
<title>js cycle by cloud-dwelling community </title>
<meta charset= "Utf-8"/> <meta
Name= "keywords" content= "JS cycle by cloud-dwelling community"/> <meta name= "description"
content= "JS cycle by cloud-dwelling community"/>

Different types of loops

JavaScript supports different types of loops:
for-loop code block a certain number of times
for/in-Looping through the properties of an object
while-loops the specified code block when the specified condition is true
Do/while-also loops the specified code block when the specified condition is true

For loop

A For loop is a tool that you will often use when you want to create a loop.

The following is the syntax for the FOR loop:

for (statement 1; Statement 2; Statement 3)
The code block that was executed

Statement 1 executes before the Loop (code block) starts
Statement 2 defines conditions for running loops (code blocks)
Statement 3 executes after the Loop (code block) has been executed


Copy Code code as follows:

for (var i=0; i<5; i++)
X=x + "The number is" + i + "<br>";

Give it a shot yourself.

From the example above, you can see:

Statement 1 Sets the variable (var i=0) before the loop begins.
Statement 2 defines the conditions for a circular operation (I must be less than 5).
Statement 3 Adds a value (i++) after each block of code has been executed.

Statement 1

In general, we use statement 1 to initialize the variable (var i=0) used in the loop.

Statement 1 is optional, that is to say, no statement 1 is used.

You can initialize any (or more) of the values in statement 1:


Copy Code code as follows:

for (var i=0,len=cars.length; i<len; i++)
document.write (Cars[i] + "<br>");

You can also omit statement 1 (for example, when a value has been set before the loop starts):


Copy Code code as follows:

var i=2,len=cars.length;
for (; i<len; i++)
document.write (Cars[i] + "<br>");

Statement 2

Usually statement 2 is used to evaluate the condition of the initial variable.

Statement 2 is also optional.
If statement 2 returns True, the loop starts again, and if False is returned, the loop ends.
Tip: If you omit statement 2, you must provide a break within the loop. Otherwise the loop will not stop. This can cause the browser to crash. Read the contents of the break in later chapters of this tutorial.

Statement 3

Typically, statement 3 increases the value of the initial variable.

Statement 3 is also optional.
Statement 3 has several uses. The increment can be negative (i--) or larger (i=i+15).
Statement 3 can also be omitted (for example, when there is a corresponding code inside the loop):


Copy Code code as follows:

var i=0,len=cars.length;
for (; i<len;)
document.write (Cars[i] + "<br>");

For/in Cycle

The JavaScript for/in statement loops through the properties of the object:


Copy Code code as follows:

var person={fname: "John", lname: "Doe", age:25};
for (x in person)
Txt=txt + person[x];

You'll learn more about the For/in cycle in the chapters on JavaScript objects.

can refer to this article concretely:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.