The other option of repeating a specific action many times is by using while loops. And to create a while loop, what you would do is you would just type while and then parentheses and inside them, you add your variable. But the variable here is no is not going to be declared as we did with for loops. But in for in my loops we need to add, we need to declare that variable before the loop. So here I'm going to add lit i equals zero. So we did we first declare the variable that is going to be increased many times.
And then here inside it, we add I and then smaller than and then the condition. This is the condition and then for example 10 because we want it to be repeated In this code, we want it to be repeated 10 times and then we add curly braces and inside those curly braces, we add our code that we want it to be repeated as long as this condition is met. So, this condition is that I is smaller than 10. And once i is greater than 10, we will get out of this loop. But remember, here we did not add a way of increasing I in for loops, the way of increasing guy is has been added inside before loop parentheses by adding i plus plus, but in while loops in order to increase I, at the end of the loop and before the closing bracket, we add i plus plus, which means that the code here will first be implemented and then I will be increased and then it is going to go all the way again to The beginning of the loop and checks if I is still smaller than 10.
If I is still smaller than 10, it is going to repeat the same code. So here is the code that we want to repeat code to repeat, which is console dot log. And then hi john, I, john, and then semicolon. And now if I save and run our program, as you can see, I get hi john 10 times 1-234-567-8910. So we get john 10 times by using a very simple code by using wild loops. So the difference between while and for is that in for loops, we have the variable declared inside the parentheses of the form of the for loop and I is going to be increased inside the parentheses of the for loop.
But in while loops, we add we declare first our integer before the loop itself and the condition is inside. But I is going to be increased from inside the, the while loop. So to recap, while loops are a great way of repeating the same code over and over again, without the need of repeating ourselves many many times and wasting our time