02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Turning around for ternary operators

As this is the last video lecture of the chapter, it's going to be really short and I hope sweet. We're going to be talking about something that isn't mandatory, but it's so cool. Ternary operators are a shorthand way to create if conditions and they are wicked.

Video Transcript

00:00Last, last video lecture of this chapter and we're going to be talking about saying that
00:05it's not necessarily mandatory, so it feels scary, don't worry about it, but it's fun.
00:11Turrenary operators, if you know how to use them, then you can impress a lot of people.
00:16It's because a lot of developers don't even know it exists.
00:18So Turrenary operators, now don't, don't be afraid of it.
00:22It's not that complicated.
00:24It just looks weird.
00:25And just make sure that if you don't get it, don't worry about it, because it's not critical,
00:31mandatory in any way or form.
00:33What I want to do is I want to add here a Turrenary operator that says this, if, it's
00:39like a if condition.
00:40I want to have here, if our time, if our time is even, then we'll have a delay of 0.5 seconds.
00:49If the time now is odd, then I want to have a longer delay.
00:53We'll have a delay of 3 seconds, okay?
00:56So let's do it.
00:57Let's, let's go ahead and do that.
00:58So I'm just going to break a line here so we can see it more easily.
01:01And you know what?
01:02Let me show you the construct of a Turrenary operator.
01:05A Turrenary operator works this way, condition, question mark, value, value.
01:15In other words, if this is true, we're going to true value, let's write here true
01:19value, false value.
01:23Very simple.
01:25So in this case, we want to have your condition.
01:27So let's set our condition.
01:28What is the condition that sets the odd hour?
01:33An odd hour would be our hour, mole do 2, when it equals 1.
01:39Because basically, if there's a leftover of 1, that means that our hour is odd.
01:44Now if that's true, what do we want to do?
01:45We want to set the delay for 3 seconds.
01:49And if it's false, we want to set a delay of half a second.
01:53And literally, I could just copy this, paste it right into the same spot that it was in
01:56there before.
01:57For visual, I don't have to add anything, but visually, if you want to make it easier
02:00to read, you could do this or something like that to make it easier to read.
02:03But it's not easy to read.
02:05And you'll see this a lot of times in a lot of programming programs, you'll download
02:10yourself.
02:11So it's good to know how this stuff works.
02:13Literally what we're doing here is we're asking if our hour divide by 2 has a leftover
02:18of 1, then return the value 3.
02:23If not, return the value 0.5.
02:25And in this case, we're sending it into our delay.
02:27And literally, we're doing this all in one line.
02:31Go back into our code.
02:32The time now is 8 in the morning.
02:34So 8 in the morning right now, there is no delay.
02:36There's only a 0.5 second delay.
02:38But if I go ahead and I send you a time that is odd, such as, let's say, 9 in
02:42the morning, we'll see that for 9 in the morning, we're going to have a 3 second
02:46delay even before we start anything.
02:50And that is it.
02:52That's all we have to talk about, Turinary Operators.
02:55And with this, we complete this chapter.
02:57The next chapter is going to be a really, really small, small, small one where we're
03:01going to do loops, which will be our last major topic in programming in general.
03:07And from there on, you know, the sky is the limit.
03:11See you next chapter.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!