In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use JavaScript's For loop". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to use JavaScript's For loop" can help you solve the problem.
Loops are handy if you need to run code multiple times, using different values each time.
Often we encounter examples of using arrays:
There is no need to write:
text += cars[0] + "
";
text += cars[1] + "
";
text += cars[2] + "
";
text += cars[3] + "
";
text += cars[4] + "
";
text += cars[5] + "
";
You can write:
for (i = 0; i < cars.length; i++) {
text += cars[i] + "
";
}
The syntax of the for loop is as follows:
for (statement 1; statement 2; statement 3){
Code block to execute
}
Statement 1 is executed before the loop (code block) begins.
Statement 2 defines the conditions under which the loop (code block) runs.
Statement 3 is executed each time the loop (code block) is executed.
examples
for (i = 0; i < 5; i++) {
text += "The number is" + i + "
";
}
From the code above, you can learn:
Statement 1 sets a variable (var i = 0) before the loop begins.
Statement 2 defines the condition under which the loop runs (i must be less than 5).
Statement 3 increments the value (i++) after each execution of the code block.
statement 1
Typically, you would use statement 1 to initialize variables used in loops (i = 0).
But that's not always the case, JavaScript doesn't care. Statement 1 is optional.
You can initialize multiple values (separated by commas) in statement 1:
examples
for (i = 0, len = cars.length, text = ""; i < len; i++) {
text += cars[i] + "
";
}
And you can omit statement 1 (e.g., set the value before the loop starts):
examples
var i = 2;
var len = cars.length;
var text = "";
for (; i < len; i++) {
text += cars[i] + "
";
}
statement 2
Usually statement 2 is used to calculate conditions for initial variables.
But that's not always the case, JavaScript doesn't care. Statement 2 is also optional.
If statement 2 returns true, the loop starts again, and if false, the loop ends.
If statement 2 is omitted, a break must be provided in the loop. Otherwise the cycle will never end. Read more about break in the next chapter.
statement 3
Usually statement 3 increments the value of the initial variable.
But that's not always the case, JavaScript doesn't care. Statement 3 is also optional.
Statement 3 can do anything, such as negative increment (i--), positive increment (i = i + 15), or anything else.
Statement 3 can also be omitted (e.g., when you increment a value within a loop):
examples
var i = 0;
var len = cars.length;
for (; i < len; ) {
text += cars[i] + "
";
i++;
}
The content of "How to use JavaScript For loop" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.