در حال بارگذاری ...

Boolean Basics and Variables0:00

Let's go over the Boolean type. Booleans can either be true or false, and are used in a lot of places, like conditionals and loops, which we'll cover after we finish the JavaScript types. Let's make a few variables here, say const, and it's common to prepend your Boolean variable names with the word is, and then any other words that describe the variable. So let's say isOld, let's say true, and let's make another one named isCool, and let's set that to false. And of course, if we console.log these variables, we'll get true or false in the console. So isOld, and isCool. You can also make use of comparisons that result in a Boolean value, let's take a look.

Comparison Operators0:36

So isOld, and isCool. You can also make use of comparisons that result in a Boolean value, let's take a look at that with numbers to start, say comparisons, and let's console.log, let's start with the greater than comparison, pretty straightforward, 4 is greater than 2, that should be true. There's also greater than or equal to, and same for less than, so let's say less than, and less than or equal to. So the first two should be true, and the last two should be false. Let me just comment this out, so we only have those four. So if we wanted to, we can make use of comparisons instead of hard coding true or false here. So let's make another variable, let's say age equals, say, 30, and we'll say a person.

So if we wanted to, we can make use of comparisons instead of hard coding true or false here. So let's make another variable, let's say age equals, say, 30, and we'll say a person is old if they are greater than or equal to 30, so we'll say age greater than or equal to 30. So in this case, isOld is still true, so let's bring this back, and let's comment out these lines, so that should still be true. But if the age were younger, say 12, then that should be false, or sorry, I meant to change the age here. Okay, now let's take a look at the equality operator, so let me comment this out, and let's say equality, and there are two types of equality operators.

Strict vs Loose Equality1:52

Okay, now let's take a look at the equality operator, so let me comment this out, and let's say equality, and there are two types of equality operators. There's strict equality, and that is the ===, so three equal signs, and there's loose equality, which is ==. So let's just console.log a few things here, and again, we'll stick with numbers for now. Let's say 4, === 4, let's try ==, and let's see what we get with that, and they are both true. So what's the difference between these two equality operators? So again, === is strict equality, so what's happening here is it's comparing the type and also the value, and it's only true if both the type and value are the same.

So again, === is strict equality, so what's happening here is it's comparing the type and also the value, and it's only true if both the type and value are the same. So in this case, the type is a number, this is a number as well, the value is four for this one, and the value is four for this as well, so that results in true. Now for the loose equality, it only checks the values, so the value of four is four, the value of four is four as well, so that results in true. However, if I change one of these to a string, again, loose equality only checks the value, so the value of four is four, the value of four as a string is still four, so if I save this, it should still be true, and it is. So if I did the same thing with the triple equals, change this to a string, now the types

Logical Operators AND/OR3:53

loose, that should be false, and it is. Now let's take a look at logical operators, let me just move this up, let's add a comment, say logical operators, and you would use these when you want to check the boolean value of more than one thing, so say for example, if you wanted to get into some sort of club for our variables up here, you have to be both old and cool. So first let's look at the operators, so let's console.log, I'll just put the boolean values in here, we have and and or, so and is represented by two ampersands, so this will only be true if both values are true, let me just put all cases here. So we have true true, true false, false true, and false false, so like I said, this will only result in true if both sides are true, so in this case, only this one, so let me

that should be true, there's one here, that should be true, there's one here, that should be true as well, but there's none here, so that should be false, so the results of this should be true, true, true, and false. Let's try that out, okay, so now let's make use of our variables up here, like I said earlier, say we wanted to check if someone's eligible to go into some sort of club or bar, then they have to be old and cool, so we would use those variables instead, again let me comment this out, let's use our variables instead, so let's say isOld and isCool, so in this case, isOld is true, or it's false because we changed the age, so this should be false and true, which will result in false, and it does, but if I change the age to someone older, then it should be true, and it's false because I also have to change isCool to true,

Wrap-Up and Next Steps8:35

look at conditionals and loops, so once we get to those videos, make sure you review everything we learned on booleans here.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟