Javascript If Else Statements | Javascript Tutorial For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back today we're gonna look at if statements so what are if statements it's a way of checking and filtering out what action we need to perform so to give an example I'm going to create a variable let's say we have a vaping website and we want to check if the user that enters our website is 18 years old so we can create a constable so let's say they are 18 H equals let's say 20 okay so what we can deal with the if statement is we can check a value to be equal to something or bigger or less than something and then we can execute a code depending on which condition they meet all right so to write an if statement all we have to do is write if and then we write parenthesis and in here we have to define our condition so something that needs to be equal to something or is it less than something or more than something okay so what we can do is say if our age is let's just say more so bigger than 18 and then we can do brackets and we can execute some code here so this is only gonna run if the age is bigger than 18 so if we canceled log here you are good to go and hit save this is gonna return us you are good to go all right now what happens if we put 10 here nothing we don't get the console.log because this doesn't meet our condition so what this happens here is basically this is gonna either return true or false so this is false right now age is not bigger than 18 it's 10 so 10 is bigger than 18 it's not it's gonna return false and it's gonna just not run this code cool but what if we want to run something if this is not true so since this returns false here we want to execute something else so again to make it sound plain English we can just say hey if age is bigger than 18 then run this code else run something else right so that's exactly what we're doing we're gonna write else and then we're gonna open up brackets again and we can write whatever we want here so since we don't reach here since our age is smaller than 18 we can do console.log you are not old enough hit save and since this is 10 we're gonna get you are not old enough great so again what's happening here is ages 10 and we're gonna say if our age is bigger than 18 which is not so this is gonna be false then it's gonna it's gonna skip this because this is false and it's gonna go into our else statement here and it's just gonna cancel like this but what if we have multiple things that we need to check what if you want to also check 21 so what we can do here is add an entry here well not for backslash enter like so and what we can do here is write else if so this is used if you check another condition okay so maybe you want to check if it's bigger than 18 or let's say we want to check if it equals to 10 for example so what we do is we just add another condition here we don't need to add a condition here because if this is not true it's just gonna go to this all right but here what else if you need to add another condition so let's just say if age is smaller than 15 like so and again we need to do brackets and we can say console.log wow you are really young why are you even here all right it's safe so what's gonna happen now is it's gonna look at our if statement is a bigger than 18 it's not so this is not gonna run it's gonna check else if age is smaller than 15 is it then is smaller than 15 it is so this is gonna run this is not gonna run anymore so truck going by again thank you so much truck peace-out shout out to the truck subscribe to this channel yeah we're gonna execute this one cool now if this was also false so if this was not correct then it would just go to the else statement so age is smaller than 15 well let's put 16 oh he's coming back come on truck let's do a collab oh and never mind it's another truck it's a blue truck hit save okay so now age is bigger than 18 it's not so it's gonna skip this part else if age is smaller than 15 it's not it's 16 so then it's just gonna go into our else statement here and what we can also do is we can check if it equals to something so we can do bigger than or smaller then we can also do equals like so so rather than doing a single equal which is usually used to assign some things when you create the variable up here you use equals 218 so you're just putting the 18 value into the age you're assigning the value again the box you remember the box we're just throwing 18 in there so you cannot do this in here because what we're doing here is we're checking the two numbers alright we're not assigning a number we're just checking and what we need to do here is it's gonna be quite tricky because we need to do triple equals alright so this is how you compare the two numbers so if H triple equals 18 then we're gonna get you are good to go okay so why do we use triple equals we can use double equals two that's gonna work just fine as you can see you are good to go but we usually use triple equals and why do we do that well because there are some bugs that you can encounter and the reason is if this is a string for example well that's gonna throw an error because 18 is not equal to the string 18 okay we want to compare the numbers not a string to a number so that's gonna that's not gonna be true so it's gonna go into run so this is false so this is why we use triple equals with two equals this is gonna be true as you can see so with double equals basically even though this is a string javascript is gonna be like oh nevermind that's good to go and we don't want to do that we always want to compare two things that are the same value type so we want to compare a number to a number we don't really want to compare a number to string because it doesn't really make sense what else can we do here well we can check if it's bigger or equal to something we can do that like so so if our age is bigger or equal like so it's save and it's gonna say you're good to go if we set this to 19 it's still gonna say you're good to go and obviously we can do smaller or equal to you so this is gonna be you are not old enough because we're on 19 but if we do 18 that's still good because we say if it's smaller or equal to okay so this is gonna be both 18 so from 18 going all the way down it's gonna be good and say we're playing Monopoly so we want to say dice 1 equals 2 let's say we roll a six and I still is gonna roll a 3 cool so what we can do here is we can also check if dice 1 let's say equals triple equals 6 and let's say I want to get the second one too so what we can do is say the end and sign like so and what we can say is dice 2 triple equals 6 so if these two are are correct then we're gonna get to this console.log so here we can say you rolled a double and here we can say you didn't like so so what this is gonna do is basically it's gonna check through both of these so it's gonna say dice triple equals 6 this is true we are getting 6 and dice 2 triple equals 6 this is false so this is not gonna run as you can see here you didn't so to get to this statement here we need to roll both of these so both of these statements need to be correct so that's when you use a + symbol here so you need to use double hand what if one value is correct so maybe you'd only want one to be correct well then you can use this so this is a normal straight line I apologize I don't know I know the correct word for this but it's next to your I mean depends on your keyboard layout and sure for me it's on the hashtag so next to the entry sign enters key on my keyboard so we're gonna use this and what this does is just gonna check one value so if one of these is correct then it's just gonna run this so we're gonna get you rolled a double even though we didn't roll a double but it's gonna check hey if this is correct or again this is called the or operator dice 2 is 6 so as long as one is correct then we're gonna get into this if function alright so yeah those are its statements hopefully you enjoy this video please drop a subscribe and I will see you in the next part hi [Music]
Info
Channel: Dev Ed
Views: 48,531
Rating: undefined out of 5
Keywords: javascript tutorial, javascript tutorial for beginners, javascript (programming language), introduction to javascript, web development, javascript if statement, if else statement in javascript, javascript if, beginner javascript, if else javascript, if js, javascript conditional, javascript conditional statements, javascript if else
Id: _5SgikgFf0U
Channel Id: undefined
Length: 11min 41sec (701 seconds)
Published: Sun Jan 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.