Summary of for loop usage in javascript

Source: Internet
Author: User

The most common one is for (Initial Value of the loop variable; cyclic condition; Incremental Value) {statement ;}
Example

[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:

[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:

[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.

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
(4) omitted expression 1, 3

[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.

[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.

[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 );

[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.

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
This is


[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.

[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.

Related Article

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.