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

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

青灯夜游
青灯夜游forward
2018-10-16 17:06:461734browse

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){
    被执行的代码块
  }

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:

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
a80eb7cbb6fff8b0ff70bae37074b813
b2386ffb911b14667cb8f0f91ea547a7Insert title here6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
8019067d09615e43c7904885b5246f0a
    for (var i = 0; i 30ec19e683c1fe835e9dea0c0830b1d0
73a6ac4ed44ffec12cee46588e518a5e

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>

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)

76c82f278ac045591c9159d381de2c57
100db36a723c770d327fc0aef2ce13b1
	93f0f5c25f18dab9d176bd4f6de5d30e
		a80eb7cbb6fff8b0ff70bae37074b813
		b2386ffb911b14667cb8f0f91ea547a7Insert title here6e916e0f7d1e588d4f442bf645aedb2f
	9c3bca370b5104690d9ef395f2c5f8d1
	8019067d09615e43c7904885b5246f0a
		var i = 0,
			len = cc.length;
		for(; i 0d5f929479e7eb9a0e12c1d901fca32f

73a6ac4ed44ffec12cee46588e518a5e

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>

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete