Now building up on what we learned
in the last lecture about storing Booleans
and having checks like this,
there is another important concept in JavaScript,
which you don't find in most programming languages,
but which is a key part of JavaScript,
and which you therefore also have to know.
Let's say we have a constant,
or a variable, doesn't matter,
"enteredUserName",
and that could be Maximilian.
It does not matter.
Now let's assume this is again a value
which we actually fetch from some input field,
just like we do it in demo.js.
There, we also get some entered value.
Let's assume the same is the case here
for "enteredUserName."
Now we might have some code that should only execute
if this is a non empty string.
And then, we could say, "Input is valid."
So, we could check if this is a non empty string,
if it has a more than zero characters.
Now for that, we can write "enteredUserName"
and get the length of that string with the length property
and check if that's greater than zero.
And if it is, which it in this case is,
then I can save this and I get, "Input is valid."
But, if that's all we care about,
there is a even shorter way of checking this.
We can also write "if enteredUserName," like that.
And if I save that, I also get, "Input is valid."
And if I go back and I turn this into an empty string,
so no input was provided,
then I actually don't get this output.
So this also works as we would want it to work.
But why?
Because JavaScript has this concept of
truthy and falsy values.
Now, we have true and false for Booleans,
but in a place where JavaScript wants a Boolean,
like in this if condition here,
if you provide a non-Boolean value,
JavaScript will basically try to convert
that provided value to a Boolean,
and it has certain rules for that conversion.
Now I got dedicated deep dive courses
where I dive way deeper into all the niche details
of how JavaScript does this conversion
and I dive into all the tricky cases
where this conversion might be strange,
but it's generally straightforward.
An empty string, or the number zero,
will be treated as false.
A string with zero inside of it
would not be treated as false
since this is not an empty string.
So empty string or zero are basically treated like false.
A string with anything in there,
even if it's the text "false,"
would be treated as true because it's not empty.
So it's really only the question of
whether it's empty or not that matters.
Even a blank character would count as not empty
and therefore as true.
And hence, if you use a string or a number here,
then this will be treated as true,
if it's not empty or not zero,
and it will be treated as false
if it is an empty string or a zero number.
And that's why this check also works.
This might sound like a niche case,
but it actually isn't.
Very often you will perform checks like this
simply because it's shorter
than checking for length greater than zero,
and it has exactly the same result.
So whilst you could do that,
this is simply shorter
and it works because of this truthy falsy behavior
that's built into JavaScript.
Now, if that's still a bit much
and a bit confusing at this point,
again, don't worry.
We'll have many examples throughout this course,
but as we are talking about if statements
and Booleans right now,
that's definitely all of the concept
that needed to be introduced at this point.