JS Loops
JavaScript Loops
JavaScript loops are used to repeatedly execute a block of code until a certain condition is met.
There are three types of loops.
- for loop
- while loop
- do-while loop
1. For Loop
A for loop is used when the number of iterations is known beforehand. It consists of three parts: initialization, condition, and increment/decrement.
The loop runs until the condition is false.
The Output
2. While loop
A while loop is used when the number of iterations is not known beforehand.
It continues to execute the block of code as long as the condition is true.
The Output
3. do-while loop
A do-while loop is similar to a while loop, but it executes the block of code at least once before checking the condition.