JavaScript for statement

Source: Internet
Author: User

For statement syntax:

for (initialization; Expression post-loop-expression) statement
Initialization expression controls expression after loop expression

Example:

var count = ten;  for (var i = 0; i < count; i++) {    alert (i);}

The above code defines the initial variable of I as 0. The code in the loop body may not be executed only if the conditional expression (I < count) is present. If the code in the loop body is executed, it is bound to evaluate the post-loop expression (i++), which is the value of increment i.

This for Loop statement has the same functionality as the following while statement:

var count = ten; var i = 0;  while (I < count) {    alert (i);    I+ +;}

The For loop does not work with a while loop. In other words, the For loop simply concentrates the loop-related code in one place.

The VAR keyword can also be used in the initialization expression for a for loop. The initialization of the variable can be performed externally, for example:

var count = ten; var i;  for (i = 0; i < count; i++) {    alert (i);}

The initialization expression, control expression, and post-loop expression in the For statement are optional. Omitting all three expressions will create an infinite loop, for example:

 for (; ;) {        // infinite loop     dosomething ();}

JavaScript for statement

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.