JavaScript: From First Steps to Professional

Logical & Ternary Operators

Anjana Vakil

Anjana Vakil

Software Engineer & Educator
JavaScript: From First Steps to Professional

Check out a free preview of the full JavaScript: From First Steps to Professional course

The "Logical & Ternary Operators" Lesson is part of the full, JavaScript: From First Steps to Professional course featured in this preview video. Here's what you'd learn in this lesson:

Anjana demonstrates using the logical operators and, or, and not to expand the flexibility of boolean conditional statements. A demonstration of using ternary operators to quickly write boolean conditionals is also provided in this segment.


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.

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.

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.

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.

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.

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.

There is a sibling operator to this one, which I see some folks are nodding. We know the next thing we're gonna need is maybe logical, or exactly and so in JavaScript that is two of the pipe or kind of vertical line operator or it is going to be true if either one of the values is true, as long as one or both of the values is true A or B is going to be true.

The only time that it's gonna be false, is if both of those values are false. So, you can just sort of, we'll be getting used to these if you haven't encountered them before. If you've encountered operators like this in other languages, it usually works the same. And there is also another operator in JavaScript that it's not a logical operator to conditional, but it's a fast way of writing conditionals and it's what we call, so we saw NOT is a unary operator it takes one operand it operates on one thing.

AND and OR are binary operators, they operate on two things. And JavaScript has one ternary operator that operates on three different values. And what this does it's a very quick way of writing an if-else statement. So, in this case, we use a condition expression same as we've been doing in our [INAUDIBLE] if statements.

And then we put a question mark like saying, is this true? And after the question mark, we put a value we want JavaScript to evaluate to this whole expression. Is gonna become the value if true if the condition is true. Then we have a colon and that's sort of like our else keyword and then we have the value that we want this to become if it's false, if the condition is false.

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.

So JavaScript I want you to evaluate this whole everything to the right of this single equal sign here all of this stuff its gonna evaluate to a single value. And the way it's going to work is JavaScript we're gonna ask it with that question mark, is this thing true is forecast triple equals sunny is the forecast sunny?

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.

So, these are kind of equivalent statements here. Cool, so, this ternary operator, if it pops up a lot in kind of real-world JavaScript you'll see it all over the place and so we're not necessarily gonna spend too much time with it but it's something that you'll get more and more familiar with as you continue adventuring into JavaScript land.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now