The best JavaScript while loop Tutorial In 2024, In this tutorial you can learn while loop,Examples,do / while loop,Examples,Compare for and while,Examples,Examples,
As long as a specified condition is true, the loop can always execute the code block.
while true loop will loop code block is executed at the specified conditions.
In this case the loop will continue to run as long as the variable i is less than 5:
If you forget to add conditions to the value of the variables used in the cycle never ends. This can cause the browser to crash. |
do / while loop is a variant of the while loop. The cycle will check whether the conditions are true before executing a block of code, and if the condition is true, it will repeat the cycle.
The following example uses the do / while loop. The loop will execute at least once, even if the condition is false it will be executed once, because the code block will be executed before the condition is tested:
Do not forget to increase the value of the variable conditions used, otherwise the cycle will never end!
If you've read the previous chapter for details about the loop, you will find that while loop like the for loop.
In this example the recycling loop for cars to display all values in the array:
In this example of recycling while loop to display all the values in the array of cars: