All right, everyone, let's continue talking about conditionals, so now I want to start talking about
evaluating values as conditions in JavaScript, just like Python PDB and many other programming languages,
we can evaluate values as true or false without booleans.
So without doing actual comparisons.
So JavaScript considers these values to be false.
So false itself, then undefined.
No, zero.
And then I'm going to talk about this in a moment and an empty string.
So if we have these values inside a test in the if statement, JavaScript is going to consider all this
to be false.
And as you will see, this could be very useful.
So first, let me just talk what is the man so every time we have an error in an operation, so let's
say we have a string with a name and we try to do something that JavaScript doesn't understand.
So we just try to do John, divided by tree, which is something completely crazy.
So JavaScript, it is going to return men, which means not a number.
No, this is actually an error.
So if there's an error, if we try to do an operation and we have this in return and we are using it
inside the if statement, JavaScript will consider this to be false and the same thing will happen for
these other values.
So now let's go to Visual Studio Code and let's see a very common example about this.
So let's say we are getting the input field.
So a person just typed his name in an input field and we have a variable called name.
And now let's say the person just left it blank so we don't have the name.
It is common to use a conditional like this.
So if name.
Then we can print something like the name is and then print the name, but if the name is left blank,
then we can print the name has not been informed.
So in this case, we have it blank.
Let's save this and let's see what happens.
So saving this and now we can see the name has not been informed.
Now let's try to change the variable.
So, Ivan.
Refresh the page again, and now we can see.
That we have the name.
So what just happened here?
We are just evaluating this value.
So instead of having to make the actual tests like if name.
Is an empty string.
We don't have to write all this because JavaScript can also evaluate values.
So in this case, JavaScript just found an empty string in JavaScript, considers an empty string to
be false, the same thing for zero.
So if we have a total.
Pop zero.
We can also do, if total.
Let's say I sold something.
Otherwise, I sold.
Nothing, so if I have a total of 100, then just by typing this refreshing the page, then we can see
I sold something.
But if the total is zero.
Refreshing the page now we can see I sold nothing.
So now we can see that these values are considered to be false, so from now on you will see that sometimes
we will just ride conditionals like this.
We don't have to actually write this test.
If name equals an empty string, we can just test the value itself.
So to finish with this lesson, let's just check another way of writing conditionals, which is the
ternary.
If so, this is the syntax.
First we type the condition, then questionmark, then the code that should be executed.
If this condition is true, then Callon and then the code to be executed if false.
So now let's get the same example we just used with the name.
So going back here.
Let me just press control to go back.
All right, so I'm just going to comment all this and I'm going to get the console log.
And inside here, instead of just writing this message, I'm just going to write the ternary if so,
how does it work?
First, I need to type the condition.
So the condition in this case, its name, if there's a name, then we can do something.
Then we can use the question mark and after the question mark.
Comes the code to be executed, if this is true in this case, we are just returning this message then
Colin and then the code to be executed.
If the condition is false.
So now let's paste the message to print in case of false.
So now we don't have a name.
Let's see what goes to the console.
The name has not been informed.
Now let's try to put a name here, Mary.
Refresh the page, and now we can see that this is working the same way, so this is good because we
can write less code to get to the same result.
So here we had a few lines of code and now pretty much if we don't consider this to be part of the code,
we just have a one line to test these conditions, which is good.
Just one more thing so we can finish this lesson we can also use or else if in this ternary conditionals.
So let's do the same example with the above with the ages.
So here we have the age example where we have three conditions.
So here it is.
Let me just copy all this.
So we have the variable and now we can just do console, not log.
And now this is going to happen in a very similar way, but we just need to pay attention first, we
want the condition.
So here we have the condition we used in the first if if this is true.
So now questionmark.
This is what we want to return.
Otherwise, so now this is not an alse this is going to be another condition.
So if you want to do this in the place where you should base the code for the house, you can just start
another condition so you can do this as many times as you want.
So now, instead of putting the code for the house, I'm just going to start another condition, which
is this one.
And now we just repeat the same thing until we are done, so in this case, this is a new condition.
So questionmark again.
Now, this is the message that we want to print in case this is true.
Again, Colin, this time we don't want another condition, so now we can just go to the last one and
print ATAT.
So this time the person is 18.
Let's try 80.
Refresh the page and now we can see Senior, let's just do one more test.
Let's try eight.
Underage and twenty eight.
We can see that this is working the same way, however, this time the fewer lines of code we achieved
came with a trade off.
Now we are making the code readability worse, because just by looking at this, it could be a bit confusing,
not in this example.
So when we have just one, if anyone else, then it's fine to use the ternary if.
But if you have multiple conditions, I personally don't like to write it like this.
I think this is confusing.
This is a bit harder to understand.
What is the logic inside here?
So that was all for this lesson.
In the next video, we're going to talk about nesting loops and conditions.
I'll see you then.