Hey, everybody, welcome back.
In this video, we are going to talk about booleans, this data type is very important, not only in
JavaScript but in any programming language.
Booleans are behind all the logic of our programs and they represent the true and false values.
So if I create a new variable here called bool.
I'm going to assign a value of true to it and then let me try to type off.
Operator of my variable.
And now, as you can see, it's telling us that this variable has a value of the type Boulin.
So we have to and we also have the false value.
Both of them are written like this without quotation marks.
And in lower case.
So let's try it again.
Let's try to type off again.
And we can see that it's also a boolean.
So what's the use of assigning a true or false value to a variable?
Actually, not so much.
But this is not how we are going to use booleans.
Actually, boolean values are going to be produced by comparisons.
So we are going to compare things.
Is this thing the same as this other thing?
Is this number higher than this other number?
And then these comparisons are going to produce a true or false value.
So here we have the comparison operators.
So we have greater than greater than or equal to let's try these two to get started.
So if I ask JavaScript if instead of comparing two numbers, because maybe that won't make any sense.
Let's create a variable for the age of a person.
So let's say the person is 16 and I'll maybe we want to ask JavaScript if this person is over 18.
So we're going to ask if age is greater than or equal to 18 and out.
JavaScript is stalling us.
No, no, this is false.
This person is not over 18.
So this is how we are going to produce these values.
So now let's say the person is 18.
And we do the comparison again.
And now it's telling us that this is true.
Now we could do the opposite.
So instead of asking if the age is greater than 18, I could ask if the age is less than 18.
So I could ask let me just assign it again just to make sure.
So now let's try 19.
Let's say only people under 18 could participate in a competition or something like this.
Maybe we have an inscription page that will only accept people younger than 18.
So we could ask, is this age less than 18?
Then it's false.
So we won't allow the person to do the inscription.
Then we also have the equal to operator.
So let's say we have those password feud's.
So let's just create a variable called password.
And now let's just try one, two, three, four.
A or something like this.
A terrible password.
But it's just for this example.
Now, let's say we have another feud asking for the person to confirm the password.
So typing it again.
So this is going to be stored in a variable called confirm.
Password.
Now, this time, the person made a mistake and just wrote one, two, three, eight.
The person forgot to Type four for now.
We are going to compare if the passwords match so we could do like is password.
The same as confirm password, and our JavaScript is stalling us that No.
This is not the same thing.
So we can't let the person proceed.
Now, let's try to change this variable.
So now confirm password is going to be the same.
So now, one, two, three, four, a..
Now doing the comparison again.
This is true.
So we can go ahead.
When dealing with comparisons, there's also the identical operator, which could be useful sometimes
because if you try to do like four as a string and if you ask JavaScript if this is the same as for
it is going to tell us that this is true.
But depending on the situation, this is not exactly true because for as a number is not the same as
for as a string.
This could be like part of a phone number.
While this is a real number.
So when we use the identical operator, this is only going to return true if both value and type are
the same.
So if we try to do it with the identical operator now, the result is false.
So now let's try again using two strings.
And now we can see that this is true.
Another cool operator is the not equal two.
So this is represented by the exclamation mark, actually exclamation marks.
They normally in most programming languages, they mean not.
So they are going to be the opposite of what you are trying to do.
So let's try this comparison again.
So I'm asking JavaScript if for let's use two numbers just to make it easier.
I'm asking JavaScript if four is the same as four.
And it's telling me that this is true.
But if I use the exclamation mark, I'm asking the opposite.
I'm asking JavaScript.
Is this thing not the same as this thing?
Well, this statement is false because those are actually the same thing.
So I don't want to overcomplicate things with this operator now.
I just wanted to show you.
And then in due time, we are going to start using it.
All right.
So now we can see that booleans can be very useful.
I just showed a few examples, like the password example of RDA age example.
But normally we are not going to use comparison operators isolated like this.
Normally we are going to use them inside conditionals.
So I have an example here to show you.
Let's go to Visual Studio code to the script.
Dot J.S. file.
So later in the course, we're going to have a lesson to talk more about conditionals.
So this is just an introduction.
I just wanted to show you this because it's impossible to talk about booleans and don't show you an
example of a conditional.
So let's say on our Web site, we offer finance options if the order value is hired and a thousand dollars.
So if the person is buying two thousand dollars, we can offer finance options.
If it's lowered and a thousand dollars, then we cannot finance the order.
So how would we do this with booleans and conditioner's?
Well, first, let's create a variable called order value.
Again, we are going to assign it ourselves.
But normally this is not what happens.
The person will choose a product that will have a value.
So then this value is going to be assigned for the variable.
But right now, just for the sake of our example, let's assign to value ourselves.
So let's say the order value is two thousand eight hundred.
So this is above a thousand.
This person is eligible for financing the order.
So to do a conditional, we are going to use the if statement.
So we are going to tell JavaScript.
If the order value is.
Greater than or equal to 1000, then we want to do something.
So this then is going to be inside curly braces.
So this is how we write conditions.
So if this thing here, this comparison returns true, then we want to go inside here and do anything
we want.
We can write multiple statements inside here.
In this case, just for our example, I'm just gonna type console dot log.
And I'm going to write you are?
Eligible.
To finance your order.
Let's do this.
Let's go back there.
Let's refresh the page.
And now we can see that we are seeing the message in the console.
On the other hand, if we had an order value of eight hundred.
Now we are not seeing anything.
So there is one more thing that you can do if this is not true.
You can choose what's going to happen otherwise, so you can use the else statement.
So if this is true, do this, otherwise do what's inside the else statement.
So in this case, I'm going to write console that log.
And I'm just going to type.
Finance.
Options.
Are not available for orders below.
One thousand dollars.
So by doing this now, we have the order value of eight hundred.
If we refresh the page, we can see that the finance options are not available.
But if we have something like twelve hundred.
Refresh the page.
Now we have finance options to choose from.
So I just wanted to show you this school example.
This is a real life situation.
This is something that we do on Web sites all the time, comparing values to see if we can offer Fang's.
You could offer like free shipping on orders above a hundred dollars.
So this is really a common thing to do.
There's more that I need to talk about conditionals.
But this is going to be done in a separate lesson for now.
That was all that I wanted to say.
So I'll see you in the next video.