for loop1. For has three expressions: ① declare loop variables; ② determine loop conditions; ③ update loop variables; the three expressions are separated by ;. The three expressions in the for loop can be omitted, but the two ";" are indispensable. 2. The execution characteristics of the for loop: judge first and then execute, the same as while 3. The three expressions of the for loop can be composed of multiple parts. The multiple judgment conditions in the second part are connected with && ||, and the first and third parts are separated by commas; for(var num = 1;num<10;num++){ console.log(num); //1,2,3,4,5,6,7,8,9 } While LoopThe expression in the while loop () can have various calculation results, but they will eventually be converted to true or false. The conversion rules are as follows. ①Boolean: true is true, false is false; var num = 1; //1. Declare loop variable while (num<10) { //2. Determine loop condition; console.log(num); //3. Execute loop operation; num++; //4. Update loop variables; } do-while loopWhile loop features: judge first and then execute; Features of the do-while loop: execute first and then judge. Even if the initial condition is not met, the do-while loop is executed at least once, which means that the do-while loop is executed one more time than the while loop. var num = 10; do{ console.log(num); // 10 9 8 7 6 5 4 3 2 1 0 num--; }while(num>=0); console.log(num);//-1 Nesting of loopsThe outer loop controls rows. The inner loop controls columns. Nested loops: solving multi-row and multi-column structures for(var j=1;j<9;j++){ for(var i=0;i<j+1;i++){ document.write("*"); } document.write("<br>"); } Traversal Methodsfor - inThe for - in statement is used to loop over the properties of an array or object. Each time the code in the for - in loop is executed, the properties of the array or object are operated on. let obj={'name':'programmer','age':'22','height':'180'}; for(let i in obj){ console.log(i,obj[i]) } for - ofThe scope of the for...of loop includes arrays, Set and Map structures, some array-like objects (such as arguments objects, DOM NodeList objects), the Generator object described later, and strings. The original for-in loop in JavaScript can only obtain the key name of the object, but cannot directly obtain the key value. ES6 provides a for...of loop, which allows traversal to obtain key values Array operations: var arr = ['a', 'b', 'c', 'd']; for (let a in arr) { console.log(a); // 0 1 2 3 } for (let a of arr) { console.log(a); // abcd } Array-like object operations: // string var str = "hello"; for (let s of str) { console.log(s); // hello } // DOM NodeList object let paras = document.querySelectorAll("p"); for (let p of paras) { p.classList.add("test"); } // arguments object function printArgs() { for (let x of arguments) { console.log(x); } } printArgs('a', 'b'); // 'a' 'b' Loop Control Statements 1. break: jump out of the current loop and continue to execute the statements after the loop. for(var i=0;i<10;i++){ if(i == 5){ break; } console.log(i); //0,1,2,3,4 } for(var i=0;i<10;i++){ if(i == 5){ continue; } console.log(i); //0,1,2,3,4,6,7,8,9 } This is the end of this article about loop methods and various traversal methods in js. For more relevant js loop methods and traversal content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Difference between src and href attributes
>>: Detailed explanation of CSS3 animation and new features of HTML5
In the interview, you should have experienced the...
As a required course for front-end developers, CS...
Table of contents 1. Introduce according to the o...
Preface As Linux operation and maintenance engine...
Installation of Python 3 1. Install dependent env...
In the previous article, it was mentioned that th...
This example takes the installation of Python 3.8...
1. Previous versions yum remove docker docker-cli...
Recently, when I installed MySQL in Docker, I fou...
Build a simulation environment: Operating system:...
Table of contents Install Dependencies Install bo...
This article example shares the specific code of ...
In the latest HTML standard, there is a calc CSS e...
Problem background: When using docker to deploy t...
Copy code The code is as follows: <html> &l...