For Loop usage in javascript _ basic knowledge

Source: Internet
Author: User
Summary of for loop usage in javascript one of the most common ones is for (initial values of loop variables; cyclic conditions; Incremental values) {statement ;}
Example

<Script type = "text/javascript"> for (var x = 0; x <10; x ++) {document. write (x);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Other usage
(1) omitted expression 1. In this case, the initial value should be assigned to the loop variable before the for statement. Pay attention to the subsequent values.
Example:

<Script type = "text/javascript"> var x = 0; for (; x <10; x ++) {document. write (x) ;}script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


(2) omitting expression 2, that is, the loop condition loop is not terminated, that is, expression 2 is always true.
Example:

<Script type = "text/javascript"> for (var x = 0; x ++) {document. write (x);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


(3) omit expression 3, but ensure that the loop can end normally.

<Script type = "text/javascript"> for (var x = 0; x <100;) {document. write (x); x ++; // at this time, x ++ is used as a part of the loop body, which is the same as position 3.} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


(4) omitted expression 1, 3

<Script type = "text/javascript"> x = 0; for (; x <100;) {document. write (x); x ++;} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


This is equivalent to while. You can use for to replace while.

(5) All three expressions are omitted.

<Script type = "text/javascript"> for (;) {document. write ("Js");} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


In this way, the loop will not be terminated. Similarly, this small while (true) {} function.

(6) expression 1 can be the initial value of the cyclic variable or other expressions irrelevant to the cyclic variable.

<Script type = "text/javascript"> var x = 0; for (var n = 0; x <100; x ++) {document. write (n); n ++;} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


Expressions 1 and 3 can also be comma expressions (including more than one expression, separated by commas );

<Script type = "text/javascript"> for (I = 0, j = 100; I <j; I ++, j --) {r = I + j; document. write (r);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


The value of the comma expression is the rightmost value in the order from left to right.

<Script type = "text/javascript"> for (I = 0; I <100; I ++, I ++) {document. write (I);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


This is

<Script type = "text/javascript"> for (I = 0; I <100; I ++ = 2) {document. write (I);} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


(7) The expression can be a logical expression or a character expression. A loop can be executed if it is not false.

Script n = confirm ("FOR") for (; n;) {document. write ("hello");} script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

From these, we can see that the for and while loops are very powerful.

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: info-contact@alibabacloud.com 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.