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