Ternary and OR
Ternary Operators are simply a consolidated if else statement condensed. In this video we will explore one example and talk a bit more about the nature of this operator. We will then show case one more tip regarding the OR operator and how it can be used in applications.
Video Transcript
00:00All right, so let's talk quickly about ternary operations.
00:04Ternary operations are basically an if statement
00:07that's consolidated, an if else statement
00:10that is consolidated into just symbols.
00:13The way that they work is very, very simple,
00:15so I just wanna show you a very short example.
00:20I'm gonna just put it as an isolated example
00:23and maybe put an extra alert here after we complete it.
00:27So let's say we have your,
00:29we wanna check if our message is empty.
00:31And if our message is empty,
00:33we wanna put one copy into it.
00:36And if it's not, we wanna put a different copy into it.
00:39So let's try this out.
00:40So basically, I'm gonna set my message to equal
00:43and then I'm gonna just create a ternary operation.
00:45But even before I do that, let me just write this out.
00:48So first of all, ternary operation works this way.
00:51Condition, then question mark.
00:54Basically, we have a condition,
00:55then we ask for that condition.
00:57If true, then we do the one side.
00:59If not, we do the other side.
01:01And it's really the syntax of it.
01:03So in our case with the message,
01:04I could ask if message equals nothing or an empty string.
01:08In that case, what do we wanna do?
01:11We'll type here the string is empty.
01:13Now the way the ternary operator works,
01:16it actually returns that value back
01:18so I could actually assign it in.
01:20And here we could say the string was not empty.
01:28All right, and if it makes it easier
01:30to visualize this whole part
01:32would probably be in a round bracket.
01:34You could add round brackets whenever you want to
01:36just to help you see things.
01:38It's not actually always needed,
01:40but you could always add extras, it's not a negative.
01:43So in this scenario, basically what's happening,
01:45we're checking if our message is empty.
01:47In our case, it is.
01:48So it's gonna change the copy or send this copy,
01:51the string is empty to our message.
01:54If our message was not empty,
01:55it would then send the string was not empty.
01:57And by the way, when I'm saying empty this time around,
02:00I don't literally mean empty,
02:02but I actually mean empty string
02:04or a string that contains no characters.
02:08So I'm gonna put here an alert for our message
02:09because our message is gonna change again in line 13 and up.
02:13And we're just gonna refresh our screen
02:15just to see what value we get back.
02:17And we definitely get that the string is empty
02:19followed by our other alerts.
02:22Now, if our string was not empty
02:24and it had some sort of content,
02:25such as let's say, just 2222,
02:28and I refresh it again,
02:29we'll see that the string was not empty
02:31followed by our regular operations of the string.
02:34And that covers the ternary operation.
02:36You don't have to work with it,
02:38but it's really important to know about it.
02:40And just by knowing about it,
02:42you'll probably save yourselves a lot of pain and anguish
02:46when you download a source code of someone else
02:48that uses that.
02:50It's less commonly seen in JavaScript
02:52and action script style languages.
02:55But you see them very, very often on server side languages.
02:58But yet it's definitely supported.
03:01And it's a very easy way to create if statements.
03:04Only problem with ternary operations
03:05that if you could actually create ternary operations
03:09that are nested inside of other ternary operations,
03:12and it could start getting really tricky.
03:14All right, so my recommendation with this one
03:17is use this sparingly, don't overuse it.
03:21And that's about it, really.
03:23That's all I have to say about ternary operations.
03:25Now, before maybe we cover this topic completely,
03:27because I know we talked about operators,
03:29but we didn't have a chance to really talk about
03:30every single possible option with operators.
03:34I wanna show you one other capability.
03:36So imagine the idea that your message
03:39was actually created externally.
03:42Let's say in our scenario that it's empty,
03:45our message come back as an empty string
03:47because it came back as a URL parameter.
03:49What if we just wanted to update the message
03:51only if our message variable was undefined,
03:55or really completely empty?
03:56And when I'm saying undefined,
03:57that means nothing is in there at all.
04:00An empty string is something.
04:01Well, undefined means it's empty completely.
04:04Undefined would be that I haven't really
04:07assigned a value to it.
04:08That would probably be a good description of undefined.
04:11And we'll talk more about those issues
04:14probably in a wrapping video later on,
04:17but really to get in and out into the details
04:20in depth, I really recommend again
04:22that you watch our longer version
04:25of the developer basics, which goes through
04:26every single step that you would have to know
04:28to become a developer.
04:30So let's just see this as like one last tip
04:32for this video before we close up this message.
04:35So this is how I would do it.
04:36I would say message equals message or other value.
04:43Now, what did I do here?
04:44Basically what I'm setting here is
04:46it's very, very similar to the way we added or
04:49into our if statements.
04:51The way that or works, although you might think
04:53that it always returns true or false,
04:55it actually returns the value of whatever is in the side.
04:59In this case, it could be or true or false.
05:02But if I had here only a variable
05:05and I didn't have your Boolean variable,
05:08I would actually get the actual value of that variable.
05:12Now there was a Boolean and again,
05:14it goes back into really needing to watch
05:16the in-depth videos to really understand it.
05:18But any value could be, if we cast it,
05:23if we kind of like convert it into a Boolean value,
05:26it could be true, it could be false.
05:27It depends on a lot of different variations.
05:29So let's see what would, first let's see
05:31what we would get back when we do an alert
05:33for our message this time around
05:35before we talk more about this one.
05:36So let me refresh that and we can see
05:38that we're getting other value.
05:40Why are we getting other value?
05:42Other value is treated as a false in this scenario.
05:46So because our string is empty, it's treated as false
05:50and thus we're never getting our message
05:53if we had some content inside, let's say one,
05:56and we would refresh our content,
05:58our ternary operator, well not really,
06:02our operator right here, our or operator
06:05would recognize that this value is true
06:07or has content within it and then assign
06:09that value directly into our variable.
06:12So that's all I had to say about really
06:14wrapping that whole holistic topic of conditions in general.
06:18I really, really encourage you, first of all,
06:20to use these more advanced features
06:23such as the one I've just shown you right now.
06:25You really have to become really comfortable
06:27with the programming language that you're working with
06:29in this scenario with JavaScript.
06:32I'm not assuming that you would be able
06:34to work with it if you've never seen it before
06:36because this title is really just to showcase you,
06:39the scenarios to showcase you those edge cases
06:41and the things that you might avoid
06:43or might notice that it is problematic for you to grasp.
06:47If this seems hard, you could avoid it
06:49but at least you're aware of it.
06:51I do recommend, however, to come back to this
06:53and if it's through our titles on zero2geek.com
06:56or through other resources to make sure
06:58that you get all the ins and outs
07:00of the kind of like the edge cases of operators.
07:04And when I'm talking about edge cases,
07:06it's about when a value is an empty string,
07:09when it's zero, when it's empty, all right?
07:12And we'll talk a little bit more
07:13about what does empty mean
07:14because there's a few different types of empty
07:16and programming languages.
07:18There's an empty which is called null or null
07:21and there's an empty that's called undefined
07:23and there's a very big difference between them.
07:25And then there's an empty of just an empty string
07:28such as opening and closing a string
07:31but without putting any real content inside of it.
07:34So it's not really empty, it's full of nothing
07:37versus null and undefined which are literally empty
07:40but it's different types of empty, all right?
07:42So that covers this one
07:45and we'll continue onward to the next topic.
