Now we understand how if statements work
and how we can use comparison operators
to check different conditions.
There is a little extension, some extra knowledge
you should have about Boolean values
and if statements though, which I want to dive in now.
And for this, I switched back to including
the conditional code JS file here in the index HTML file.
And in this conditional code JS file,
I now want to show you a different way of using
Boolean values and of writing if statements,
which is also very common in Javascript.
Up to this point, we always derived Boolean values
by writing comparison code like this.
And that is super common.
But sometimes you also have so-called flag variables,
which have a true or false value stored inside of them.
And an example could be that you're building a website
where users can log in and hence in your Javascript code
that adds a certain functionality to the page,
you might be storing the current log in status of the user
because your code execution depends on whether
a user isLoggedIn or not.
For that, you could have a constant here or a variable,
doesn't matter, which you could name isLoggedIn,
which is a kind of name we haven't used up to this point,
but which is a naming convention you'll use very often
when you store Boolean values,
because you want to describe what's stored in a variable.
And here I want to store true, let's say.
So not the result of some comparison, not a number,
not a string, but true as a value itself.
And then isLoggedIn is a quite fitting name
if this variable stores the answer to the question
whether the current website visitor is logged in or not.
So here I'm storing true. Could be false, doesn't matter.
It's just an example in that variable named isLoggedIn.
And now we might have some code that should only be executed
if this is true.
And here, of course, it's hard coded,
but again, we could be fetching this from some other part
of this website and hence this could be dynamic.
Let's imagine it is.
Now we can write a if statement and we can, of course,
then for example, console log user is logged in.
We could do that.
Now in this condition, we now might want to check
if isLoggedIn is true or false.
And for that, we can of course compare isLoggedIn
to true and say this code should execute if isLoggedIn,
this variable, has stored true as a value.
That would work and there's nothing wrong with it.
But keep in mind this condition here, in a if statement
or also in an elseif block, of course,
wants a Boolean value in the end.
And we use comparisons like this,
or like this because those comparisons do yield
Boolean values.
Now isLoggedIn actually already has
a Boolean value stored.
So this is kind of a redundant comparison.
We can also just write if isLoggedIn like this.
And if that's true, which in this case is,
this code will execute.
If this would be false, then this code would not execute.
So if I save that, I get user is logged in here.
Now let's say we would want to have the opposite check.
And we want to say user is not logged in here.
Then we might want to check whether isLoggedIn is false.
And again, we could therefore write this
and that would be correct, but we can also shorten this
and just write if not isLoggedIn
with that negation operator,
this exclamation mark.
With that we're saying, if this is not true,
then this code should execute.
Here, of course, it is true.
And therefore if I save that, we have no output from that
if statement here because we don't make it
into this if statement with this condition and this value.
But knowing that you can store Booleans in variables
as I'm doing it here, and how you should then name
these variables, that you should name them
as if they answer a question, and that this is also
a common thing to do and how you can then
use these Boolean values in if checks, that is important.
Because again, that's also a certain way of working
with Booleans and if statements,
which you will see a lot in web development.