Transcript from the "Logical & Ternary Operators" Lesson
>> So, now, sometimes what we care about is not the value that we have itself, but the opposite of that value. So, for example, if someone is around you is false, then we might say, as Destiny's Child once did if no one is around you, so if it is not true that someone is around you, then you should say, baby, I love you, as we were told in the epic song, Say My Name.
[00:00:30] So, sometimes we care about the opposite of the value, and the exclamation point, which is the logical NOT operator, gives us the opposite of a Boolean value. Or the opposite of the truthiness of a another type of value. Cool, so we can use NOT to test the opposite of something.
[00:00:56] And sometimes we care about more than one value and we care about their kind of the truthiness of both of them. So, for example, if you're happy and you know it, that's when you should clap your hands. And so, to care about more than one value, we have some of these logical operators.
[00:01:17] So, NOT the NOT operator, the exclamation point is a logical operator too, it operates on one value, or we could say it's a unary operator, you might see that word around. And then this double and sign, this double ampersand that is another operator that is what we call a binary operator.
[00:01:36] Binary logical operator operates on two values, and so these type of logical operators, let us make two boolean values become one boolean value. The entire expression takes two values looks at their truthiness and evaluates to true or false. So it's a little bit more complicated than that but we can think of it that way for now.
[00:02:00] So logical and is this double ampersand operator, and it is only going to be true if both values are true. So what we have is a nice little truth table here just showing if we have A and B. That is only going to be true if both A is true and B is true if either one of them is false or both of them are false, then A and and B, or we usually just say A and B is gonna be false.
[00:04:28] So, for example, let's say I have a forecast variable that could be sunny or rain or whatever and I want to know what my mood is going to be based on the weather. I can do a line like this. I can say my mood, I'm going to assign it to the value of this whole expression, that is created by this ternary operator.
[00:05:10] And if so, then this expression will evaluate to the string happy, which is gonna get saved to my mood variable, and if not if the forecast is not equal to sunny then the whole expression is gonna evaluate to the value sad and my mood will be sad. And this is equivalent to doing the same thing if we were to declare a variable but not assign it and then assign it in our if-else block.