#13 Ternary Operator in JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is evan reddy and let's continue with the series on javascript in the earlier video we have talked about if else right so we have seen if else and then we have seen if else if else now in this video let's go beyond e files and let's talk about another way of checking for the conditions and assigning value okay so what i'm saying is let's say i have a different way so this is the last example let's remove this and let's have a different scenario this time so what we will do is i will find a number is even or odd i guess that was your assignment question right let's try that here so let's say we have a number you have to find if it's even or odd and the result has to be stored in a variable and you can print it later okay so what i'm saying is let's say we have a variable which is num and the value for num is 5 and we know 5 is an odd number but then there's a way you can find that right there's a formula which you have to run so normally in your brain you do that by default but here also we have to run a formula one you have to run a calculation to find okay now if it is even i will save that in a string called let's say result so initial result will be undefined of course right if you don't assign a value which is undefined so here if you try to print result it will be undefined okay so based on the value of num i want to assign is it even order now the way you can do that we have done that before right so first of all how do you find uh even odd numbers so if you divide a number by two and if you get the remainder as zero it's an even number if you divide the number by two and if you get one it's an odd number now in this case let's try that so we'll say if and we can write the statement in the if condition itself so we'll say if num mod 2 that's how you do it right when you want to find a remainder you say mod 2. and now you want to compare this with a number 0 or 1. so we'll start with 0 i will say equal to equal to so remember we have to use 3 equal to s if you want to compare so we'll say equal to equal to 0 if that is a scenario uh you will assign the value result right so you will say result is equal to and you will say even right so that's a string you want to assign to but what if it is false in this case you will say else and you will say result is equal to or so if it is true you will say even if it is false you will say odd that's the only thing you want to do right now once you have assigned the value at the end you can come back here and say log and you can simply print result that's the simplest thing you want to do in fact you can have return the log inside evals but this is one of the way you can do it right now once you've got your code let's run this so if you underscore you can see we got order that makes sense right because 5 is a odd number but what if you get a number let's say 8 now in this case it should print even so this is working now if you want to achieve the same thing there's a shortcut to it so if you know that you just want to compare a condition and based on that you want to assign either even or order so not just for even odd but if any scenario where you have a condition and you have two options either you can go with this or that in that case you can also use a ternary operator this looks like this question mark and colon okay so how do we use this so what i'm saying is instead of using the entire stuff so i don't want to use this i will use a comment so using double slash you can give a comment okay so instead of using this i just want to use a special operator or ternary operator which you can call is question mark and a colon now how do we use this see ultimately i want to assign a value to a result right so if you look at this effects the idea is to assign even or odd to result okay so i will do that i will say result is equal to okay so i want to assign either even or odd so how do we define that it is even or order so i want to assign result it should be even or odd based on a condition and you can assign the condition here so you can put a question mark before it and before the question mark you have to assign your condition so this is num mod 2 is equal to equal to equal to 0 that's it so we are checking for the condition if this is true it will execute the block after this question mark so this will be executed if this is false the condition is false in that case it will print the thing after the colon which is the order okay and let's try so you can see that how many lines we have saved if i remove the entire comments okay let me just reuse seven and you can see we got order so you can see the code here so we got uh if else which takes four lines and you are doing that on one line that's the power of a ternary operator so that's it from this video i hope you enjoyed that's how to operator works in the next video we'll see something else till then hit that like button and do subscribe for further videos bye you
Info
Channel: Telusko
Views: 9,644
Rating: 4.9675674 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: VBCOx4CqBz0
Channel Id: undefined
Length: 5min 2sec (302 seconds)
Published: Wed May 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.