Now, the last kind of loop
I mentioned on the slide was a while loop.
And with the for loops,
we basically always had like a fixed number of times
that loop executed
either because we defined it here
or because we had a fixed amount of array elements
or properties in an object.
With while, we instead just have a condition
on which we depend.
And as long as that condition is true, we continue.
Now, let me show you an example.
We write a while loop by using the while keyword,
then parentheses between which we can put our condition,
and then curly braces.
As long as the condition we put in here is met,
we'll continue executing this code.
So if I would write while true here,
we would create an infinite loop
and therefore for you should not saved this
because this will crash your website
since it will never end.
Instead, we could be doing something like this.
We could add a variable, "isFinished,"
and that could store a Boolean value,
so initially it could be "false."
Here in the while loop,
we then check for the opposite.
We want to keep on running as long as we are not finished,
hence we can execute "not isFinished" here.
And in here,
we could now show a confirmation dialogue to the user
with help of the confirm function.
It's basically an alternative to alert,
but confirm allows us to answer with yes or no.
And therefore it will return a Boolean value.
We can then enter the message
that should be shown to the user like,
"Do you want to quit?"
And this will open up an alert box,
which already has a yes or no button.
And hence, since this will return a Boolean,
we can store this in "isFinished"
and override this "isFinished" variable value
with the response of the user here.
And if the user chooses,
"Yes," he or she wants to quit,
"isFinished" will be true,
and then "not true" will be false,
and the while loop will quit.
So thereafter, we could "console.log done",
but that will only execute if that while loop stops.
Because as long as this keeps on executing,
this code will not be executed.
So if you save that now and you reload this page,
you get to this prompt, "Do you want to quit?"
And if I click on "Cancel,"
which means no, basically,
I get this prompt again until I finally click, "Okay."
And, at that point, we get done down here
and we don't get this confirmation box anymore.
And again, it's still just a dummy example,
but it shows us really well how a while loop works.
And these are the key loops we have in JavaScript.
Now that we explored them in theory,
let's have a look at more concrete, realistic examples.