What are the three methods of JavaScript looping?

青灯夜游
Release: 2021-11-04 15:25:53
Original
12457 people have browsed it

Three loop methods of js: 1. while loop, syntax "while (conditional expression) {statement block}"; 2. "do-while" loop, syntax "do{statement block}while( Conditional expression)"; 3. for loop, syntax "for (variable initialization; conditional expression; variable update) {statement block}".

What are the three methods of JavaScript looping?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

When we use JavaScript, we often encounter the need to run the same code over and over again. This is a waste of time and inefficient. Using loops is a wise choice, which greatly improves efficiency and reduces code. quantity.

There are three types of loops in JS:

1, while loop

2, do-while loop

3, for loop

1. While loop

1. Grammatical structure of while loop:

while(条件表达式){
当条件表达式为布尔值true时要执行的语句块
}
Copy after login

2. Application of while loop

while Loops are often used in situations where you don't know the number of loops, such as asking the user to loop through an integer until a special character is entered. You have no way of knowing the number of times the loop will occur. For example:

What are the three methods of JavaScript looping?
What are the three methods of JavaScript looping?

2. do…while

#1. The grammatical structure of do…while:

do{
条件表达式为true时执行的语句块
}while(条件表达式)
Copy after login

2. Application of do...while

The difference between do-while and while loop is that it first executes the statement in the loop, and then determines whether the expression is true. If it is If true, the loop continues; if false, the loop is terminated. Therefore, the do-while loop must execute the loop statement at least once. As follows:

What are the three methods of JavaScript looping?

##3. for loop

1. The syntax structure of for loop:

for(变量初始化;条件表达式; 变量更新){
条件表达式为true时执行语句块
}
Copy after login
2. Application of for loop

The for loop is mostly used in situations where the number of loops is relatively clear. It is the kind that can be seen at a glance how many times it needs to be looped. It is relatively intuitive. The first sentence of the for loop contains the initialization of the variable and ends the loop. Conditions and each updated value, the actual thing to be done is executed inside the loop body. For example, for(n=1;n

What are the three methods of JavaScript looping?

3. Transformation of for loop

  • for-in---Loop through the properties of the object

  • forEach---ES5 introduces a new loop

  • for-of---ES6 introduces a new loop

4. Compare the differences between the three

1. while judges the condition first and then executes the loop body. If the initial conditions are not met, the while loop body will not be executed (judge first and then execute)

2. Do-while executes the loop body first and then determines the condition. Regardless of whether the condition is satisfied or not, the loop body is executed first. (Execute first and then judge)

3. For judges the condition first and then executes the loop body. It is generally similar to while. Use for when the number of loops is determined.

[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of What are the three methods of JavaScript looping?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template