What are the basic loop types in js

青灯夜游
Release: 2018-12-12 09:16:17
Original
6370 people have browsed it

The basic loop types of js are: for loop, while loop, do-while loop, and for-in loop.

This article will introduce to you what the basic loop types of js are and how to implement loops, so that everyone can have a simple understanding of js loops. I hope it will be helpful to you.

What are the basic loop types in js

The loop types supported in JavaScript can basically be divided into four types: for loop, while loop, do-while loop, and for-in loop. Below we will discuss Let’s introduce these four cycle types in detail. [Recommended related video tutorials:JavaScript Tutorial]

js for loop

The for loop first determines whether the condition is True, then execute the code block in {} (if the code block in {} has only one statement, {} can be omitted).

Function: When the number of loop iterations is known, you can use it to loop through a block of code a fixed number of times.

Syntax:

for(表达式1;表达式2;表达式3) { 要执行的代码块 }
Copy after login

Description:

Expression 1: Declare the variables of the loop and initialize the variables.

Expression 2: Judgment condition of the loop

Expression 3: The increment of the loop is a variable used to update the loop (can be incremented or decremented)

Note:Multiple expressions in the for loop need to be separated by semicolons ";", and the expressions in the for loop can be omitted, but there must be two ";" exists and cannot be omitted, that is, it can be in the form of for(;;).

Execution flow chart:

What are the basic loop types in js

Example: Simple example of for loop

Copy after login

Rendering chart:

What are the basic loop types in js

In this example, a variable i is declared and a value of 1 is assigned to variable i; {} can only be executed when the value of variable i is less than or equal to 5. statement block; each time the for loop ends, the value of variable i increases by 1.

js while loop:

while loop also first determines the role of executing the specified code block

: When the specified conditional expression is true, loop the specified code block; when the number of loop iterations is not known, you can use it to loop through an infinite number of element code blocks.

Syntax:

while(条件表达式) { 要执行的代码块 }
Copy after login

Note:In the conditional expression in the while loop, no matter what the result of the conditional expression is, type, will eventually be converted into logical values: true and false.

Execution flow chart:

What are the basic loop types in js

Example: A simple example of while loop

Copy after login

Rendering chart:

What are the basic loop types in js

In order to prevent the while loop from becoming an infinite loop, an "increment" will be added to the execution code block of the while loop to update the judgment loop variable.

do-while loop:

The do-while loop is executed first and then judged, regardless of whether the result in the conditional expression is true or false , the code will be executed at least once.

Grammar:

do{ 要执行的代码 } while(条件表达式);
Copy after login

Execution flow chart:

What are the basic loop types in js

##Example: Simple example of do while loop

Copy after login

Running result:

What are the basic loop types in js

for-in loop:

Function: Mainly used Loop through the properties of the object

Syntax:

for(keys in zhangsan) { 要执行的代码 }
Copy after login

Example:

var obj = { a: 1, b: "lian" }; //给obj定义一个不可枚举的属性c Object.defineProperty(obj, "c", { value: 2, emumerable: false, writable: true, configurable: true }); //虽然属性c不可枚举,但是值依然存在 console.log(obj.c); //2 for (var i in obj) { //只会遍历可枚举属性 console.log(obj[i]); //1 lian }
Copy after login
Running results:

What are the basic loop types in js

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of What are the basic loop types in js. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!