JavaScript is a literal translation script language, a dynamic type, a weak type, a prototype based language, and a built-in support type. Its interpreter, known as the JavaScript engine, is widely used as a scripting language for clients, and is used in HTML (an application under standard Universal Markup Language) to add dynamic functionality to HTML Web pages.
JavaScript has been born for more than 20 of years, and the way we've been using it to loop through an array is this:
for (var index = 0; index < myarray.length; index++) {
console.log (Myarray[index]);
}
Since JavaScript5, we can start using the built-in ForEach method:
Myarray.foreach (function (value) {
console.log (value);
});
Writing is a lot simpler, but there are drawbacks: you can't interrupt loops (using statements or using statements.)
There is also a looping method in javascript:.
The for-in Loop is actually designed for looping "enumerable" objects:
var obj = {a:1, b:2, c:3};
For (var prop in obj) {
console.log ("obj." + prop + "= + Obj[prop]);
}
Output:
//"OBJ.A = 1"
//"OBJ.B = 2"
//"OBJ.C = 3"
You can also use it to loop through an array:
For (var index in myarray) {//not recommended for such
Console.log (Myarray[index]);
}
It is not recommended to loop an array with for-in because, unlike objects, the index of an array is not the same as a normal object attribute, and is an important numerical sequence index.
In short, for–in is a way to loop through an object with a string key.
For-of Cycle
JAVASCRIPT6 introduces a new cyclic approach, which is the for-of loop, which is both simpler than the traditional for loop and makes up for the short boards of foreach and for-in loops.
Let's take a look at its for-of syntax:
for (var value of myarray) {
console.log (value);
}
For-of's syntax looks very similar to For-in's, but its function is much richer, it can loop a lot of things.
For-of Cycle Use Example:
Let iterable = [ten, a];
For (let value of iterable) {
console.log (value);
}
30 per//
We can use it instead, so it becomes a static variable that is not modifiable in the loop.
Let iterable = [ten, a];
for (const value of iterable) {
console.log (value);
}
30 per//
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
Loop a DOM collections, such as nodelist, before we discussed how to cycle a nodelist, now convenient, you can use the for-of loop directly:
Note:this'll only work in platforms, have
//implemented Nodelist.prototype[symbol.iterator] let
article Paragraphs = document.queryselectorall ("article > P");
For (let paragraph of articleparagraphs) {
paragraph.classList.add ("read");
}
Loop an object that owns the enumerable property
For–of loops cannot be used directly on ordinary objects, but if we loop by the attributes that the object has, we can use the built-in Object.keys () method:
for (var key of Object.keys (Someobject)) {
Console.log (key + ": + Someobject[key]);
}
Loop a generator (generators)
We can loop through a generator (generators):
function* Fibonacci () {//a generator function let
[prev, curr] = [0, 1];
while (true) {
[prev, Curr] = [Curr, prev + curr];
Yield Curr
}
}
for (Let N of Fibonacci ()) {
console.log (n);
Truncate the sequence at 1000
if (n >= 1000) {break
;
}
}