Home >Web Front-end >JS Tutorial >JavaScript For Loop_Basic Knowledge

JavaScript For Loop_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 19:15:421110browse

Loops in JavaScript are used to execute the same piece of code a specified number of times (or when a specified condition is true).
JavaScript Loops
When writing code, you often want to execute the same piece of code repeatedly. We can use a loop to accomplish this function, so we don't have to write several lines of the same code repeatedly.
JavaScript has two different types of loops:
for
Loop a piece of code a specified number of times
while
Loop and execute the code when the specified condition is true for loop
in Use a for loop when the number of runs of the script has been determined.
Syntax:

Copy code The code is as follows:

for (var=start value; var<=end value;var=var step value)
{
Code to be executed
}

Example:
Explanation: The following example defines a Loop program, the starting value of i in this program is 0. Each time the loop is executed, the value of i will accumulate by 1, and the loop will continue to run until i equals 10.
Note: The step value can be negative. If the step value is negative, you need to adjust the comparison operator in the for statement.

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Result: The code is as follows:


The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
The number is 6
The number is 7
The number is 8
The number is 9
The number is 10
Statement:
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