The loop in Javascript is used CodeSpecifies the number of executions (or when the specified condition is true ).
Javascript loop
When writing code, you often want to execute the same piece of code repeatedly. We can use loops to complete this function, so that we do not need to repeatedly write several lines of the same Code.
Javascript has two different types of loops:
For
Number of times a piece of code is cyclically executed
While
When the specified condition is true, the code for loop is executed cyclically.
Use the for loop when the number of scripts is determined.
Syntax:
Copy codeThe Code is as follows: for (Var = start value; var <= end value; Var = var + step value)
{
Code to be executed
}
Instance:
Explanation: The following example defines a loop.ProgramThe starting value of I in this program is 0. Every time a loop is executed, the value of I will accumulate 1, and the loop will continue to run until I is equal to 10.
Note: The step value can be negative. If the step value is negative, you need to adjust the comparison operator in the for declaration.