Home > Web Front-end > JS Tutorial > Introduction to for loop and for/in loop in JavaScript learning

Introduction to for loop and for/in loop in JavaScript learning

青灯夜游
Release: 2018-10-16 17:07:48
forward
1768 people have browsed it

This article will introduce to you the for loop and for/in loop in JavaScript learning. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Loops can execute a block of code a specified number of times.

JavaScript supports different types of loops:

  • ##for - loops a block of code a certain number of times

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

  • while - When the specified condition is true, loop Specified code block

  • do/while - When the specified condition is true, loop the specified code block

##for loop

Syntax of for loop:

for(语句1;语句2;语句3){
    被执行的代码块
  }
Copy after login

Description:

Execute before statement 1 (code block) starts

Statement 2 Defines the conditions for running the loop (code block)

Statement 3 Execute after the loop (code block) is executed

For example:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
    for (var i = 0; i < 5; i++) {
        alert("我是for循环");
    }
</script>
</html>
Copy after login

From the above example, it can be seen:

Set the variable before the loop starts (var i = 0; )

Define the conditions for the loop to run (i must be less than 5)

Increments a value (i )

Generally use statement 1 to initialize the variables used in the loop ==》(var i = 0;)

You don’t need to write it either Statement 1, you can initialize any number of values ​​in statement 1.

You can also omit statement 1, for example, the initial value has been defined before the loop.

For example:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
	</head>
	<script type="text/javascript">
		var i = 0
		for(; i < 5; i++) {
			alert("我是for循环");
		}
	</script>

</html>
Copy after login

But the ==》; after statement 1 cannot be omitted.

Statement 2 is used to evaluate the condition of the initial variable. Statement 2 is also optional. If the statement returns true the loop starts again, if it returns false the loop ends.

If statement 2 is omitted, name must provide a break within the loop, otherwise the loop cannot be stopped and the browser may crash.
Statement 3 usually increases or decreases the value of the initial variable. Statement 3 is also optional and has many uses. The increment can be a negative number (i--) or greater ( i = i 15)

Statement 3 can also be omitted, (for example, when there is corresponding code inside the loop)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
	</head>
	<script type="text/javascript">
		var i = 0,
			len = cc.length;
		for(; i < len;) {
			alert("我是for循环");
			i++;
		}
	</script>

</html>
Copy after login

for/in loop

JavaScript for/in statement loops through the properties of the object:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
	</head>

	<body></body>
	<script type="text/javascript">
		var person = {
			name: "haha",
			age: 16
		};
		for(x in person) {
			text = text + person[x];
		}
	</script>

</html>
Copy after login

For/in in JavaScript can not only Traverse the properties of an object and also traverse an array.

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

JavaScript Video Tutorial

, jQuery Video Tutorial, bootstrap Tutorial!

The above is the detailed content of Introduction to for loop and for/in loop in JavaScript learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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