Simple learning JavaScript for Statement loop structure _ basics

Source: Internet
Author: User

You can see the example directly, it's too much, it's simple.

(function () {
  for (var i=0, len=demoarr.length; i<len; i++) {
    if (i = = 2) {
      //return;  function execution is terminated
      //break;  The loop is terminated
      continue;//loops are skipped
    };
    Console.log (' demo1arr[' + i + ']: ' + demo1arr[i]);
  }
();

For the For loop, there are a few points to note

    • I in the For loop still exists after the end of the loop and in the scope, in order to avoid affecting other variables in the scope, use the function itself to isolate () ();
    • Avoid using for (Var i=0 i<demo1arr.length; i++) {} so that the length of the array is computed every time, less efficient than the way it was above. You can also perform a variable declaration in front of for to improve reading
    • var i = 0, len = demo1arr.length;
    • for (; i<len; i++) {};

There are several ways to jump out of the loop

    • The return function execution is terminated
    • Break loop is terminated
    • The continue loop is skipped.

Full example:

<! DOCTYPE html>
 
 

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.