Fully parse the For loop in JS

Source: Internet
Author: User

JavaScript has been in existence for more than 20 of years, and the way we've been using it to loop an array is this:

for (Var index=0;index<myarray.length;index++) {

Console.log (Myarray[index]);

}

Since JavaScript5, we have been able to use the built-in ForEach method:

Myarray.foreach (function (value) {

Console.log (value);

});

The wording is a lot simpler, but there are many drawbacks: you can't interrupt the loop (using break or Continue)

for-in is actually designed for looping "enumerable" (enumerable object) objects.

var obj={a:1,b:2,c:3};

For (var prop in obj) {

Console.log ("obj." +prop+ "=" +obj[prop]);

}

// 输出: // "obj.a = 1" // "obj.b = 2" // "obj.c = 3"It can also be used to loop an array: It is not recommended to do so in a for-in function where the variable appears as a string, when manipulating a[x+1 in a function] is not valid, and x+1 string concatenation. Because: the index of an array is not the same as a normal object, the most important numeric sequence indicator in summary: for-in is used to loop a method with a string key objectfor-of CycleJAVASCRIPT6 introduces a new loop method, that is, the for-of loop, which is simpler than the traditional for-loop code, while compensating for the short-plate for (var value of MyArray) {Console.log () of the foreach and for-in loops value);}  For-of's syntax looks similar to for-in, but her function is much richer and can loop a lot of things for-of cycle use example: Let Iterable=[10,20,30];for (let value of iterable) { Console.log (value);//10 20 30} We can use overrides so that he becomes a non-modifiable static variable in the loop let Iterable=[10,20,30];for (const value of iterable) {C Onsole.log (value);//10 20 30} Loops a string:

Let iterable = "Boo";
For (let value of iterable) {
Console.log (value);
}
"B"
"O"
"O"

let iterable = new Uint8Array([0x00, 0xff]); for (let value of iterable) { console.log(value); } // 0 // 255 let iterable = new Map([[ "a" , 1], [ "b" , 2], [ "c" , 3]]); for (let [key, value] of iterable) { console.log(value); } // 1 // 2 // 3 for (let entry of iterable) { console.log(entry); } // [a, 1] // [b, 2] // [c, 3] let iterable = new Set([1, 1, 2, 2, 3, 3]); for (let value of iterable) { console.log(value); } // 1 // 2 // 3

Loop a DOM Collection

Looping a DOM collections, such as NodeList, before we discussed how to loop a NodeList, now convenient, you can directly use the for-of loop:

let articleParagraphs = document.querySelectorAll( "article > p" ); for (let paragraph of articleParagraphs) {   paragraph.classList.add( "read" ); }

Fully parse the For loop in JS

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.