Loading
Current section: 4. Control Flow 8 exercises
solution

Ternary Operator

Transcript

00:00 Okay. Let's get into ternary. So we're gonna start with our temperature, 25. We want the weather description, to, to say here. So we want our weather description using the ternary operator to be hot.

00:12 If it's greater than 30, otherwise, comfortable. And so, of course, our AI agent knows exactly how to do this. So let's just talk about the results here. So, this first part or a ternary comes in three parts. First, there's the condition block and then this is the, what's called?

00:29 Oh, what's it called? It's the value if true. It's the consequent. That's what we call this. So if this, expression here is true, then, what the entire expression will evaluate to is hot.

00:42 Otherwise, this is the alternate. So if this expression is not true, then the value of the entire expression will result to comfortable. And so this entire thing is an expression, but then you've got an expression here, an expression here, an expression here. So expressions nested all over the place, versus statements, which is this entire line that includes a variable declaration. Or if we were to do an if statement, this would be a statement here with embedded expressions as well.

01:12 But, or actually, no. These are sorry. This is also a statement. The expression would just be right here. But, yeah, by having a statement, it means that you can't, pass it as a function argument and various other things.

01:25 And so that's why I really like ternaries because it's just a really expressive way to, like, have some conditional logic, some control flow in the context of where you're working rather than having to take a statement, break it out into multiple statements, and then put them all together at the end. Okay. Great. So that's our weather description. Now, we're going to say, our if our score is 85, then our passed I'm waiting for the AI to fix it, but I think it's a little, gun shy because I'd stopped it.

01:57 There we go. So, our score is 85 if we're we are passed if the score is greater than or equal to 70. And you're seeing the AI is saying, hey. You know what? We don't need the or true or false, thing here.

02:12 And that's what kind of what I why I included this example because I want you to I see this sometimes in actual production code. I want you to think about this for a little bit. Here, we have an expression that evaluates to true or false. If this is true, then we're going to say, this entire expression evaluates to true. Otherwise, the entire expression evaluates to false, which is the exact same as just the expression by itself.

02:37 So you don't typically want to have a ternary that uses true and false. And in fact, there are, editor plugins and even, command line interface, tools called ESLint or or linter tools that, will prevent you from doing this exact thing because it's pretty pointless. In fact, you could even argue that Prettier, could consider this something that that it would format away because the ultimate result is gonna be the same. So avoid that, if you can. Alright.

03:07 So then let's, do another example here. Our stock, if it's at zero, then our stock message, using the ternary, should be in stock or out of stock based on whether the stock is greater than zero. So if it's greater than zero, then we've got this in stock. Otherwise, it's out of stock. And let's do this.

03:26 Save that. There we go. Our temperature is 25. We're feeling comfortable. The score is 85.

03:30 You passed. Hooray. Stock is zero, so we're out of stock. There you go. And of course, you can play around with all these numbers and everything.

03:37 But that's the basic idea of ternaries. They are just this portion right here is the ternary. They are expressions. And actually, here here's something fun we could do if, whoops. We'll put all of this inside of a parenthesis, hot, then, or we can console log log.

04:00 Yay. There we go. And we get our console log. Oh, we we are not getting our console log because the temperature is 25. Yeah.

04:09 Boo. There we go. One other thing that I failed to mention here for, my American friends and others, this temperature is 25. That would be very cold for us. So clearly, this temperature is measured in Celsius, which for varying reasons, I have mixed feelings about whether Celsius or Fahrenheit is better.

04:29 I probably should have called that out. Maybe I'll add a comment here so that you're not confused like, no. 30 degrees is is not hot and 25 is not comfortable. So anyway, there you go. That is ternaries.

04:40 Really big fan of ternaries. I hope that you are also good job with this exercise.