JavaScript Basics Pt. 13 - If Else Statements

JavaScript
Transcript

English (Auto-generated)

Welcome back everyone to Javascript basics, we are here with part 13 and we are diving into if else statements. So it's time to get into more logical javascript and it's time to dive deeper. So there is a reason that javascript is so popular across the internet and people have found ways using tools like no Js to use javascript um other places that they can, even places that weren't designed to have javascript be used because it's just so popular and commonly used. And one of the things that makes javascript such a diverse language is conditional logic. So what if you want to run some code but only if a certain condition is met, for example, if it's morning, what if you want a web page to tell the user good morning or if it's night, you want to tell them good night or maybe to get a little more advanced, you only want to show them certain things if they're logged in or if they have a certain type of account, something like that. So that is where if else statements can come into play and if our statement is a block of code that will execute depending on certain conditions that you give it. So if the state is true, run a piece of code you tell it to. But if it's false, you can have run something else instead. So to simplify it and break it down. So if the condition is true, execute this specific code else, execute a different piece of code. So it's really that simple um it really is just going to be just do this. If this is true if it's not true, do this instead. So let's get into the syntax of it and see how that's written out by writing out our first if else statement together. All right. You want to go ahead and open up your replicate. If you don't already have it open already, you don't have your project up or your previous project just opened up a brand new html CSS and javascript project from the front page head to that script dot Js file. Let's dive in. So we are going to be writing this in a function. So I'm going to play a function that you normally would and let's just go ahead and call this one greeting because we're gonna go uh we're gonna write a if else statement. Uh um one of these situations I was talking about before where I will say good morning or good night except we're just going to really simplify this. We're not gonna go based on time instead of trying to grab the time from their computer or something, we are just going to use some simple numbers. So our function will be called greeting. We're gonna pass at one parameter called time and curly braces after that. And then so this is really if else statement comes in, you're right if The parentheses and inside these parentheses is where you will put your condition to run if it's true, so if time is less than 50 and then so you're right your if condition inside the premises then you follow that with curly braces and then here you're right what you wanted to do. So for us we wanted to give it give the user a specific reading separate console dot log. Good morning. So if it's less than 50 it's the morning. So Right, good morning. And then after that sort of curly braces, your white house and then follow else with another set of curly braces. And then this is the piece of code you want to execute if it's false. So else for us we'll have a console log. Goodnight. Alright. So let's go ahead and call our greeting function down here because it has a parameter will want to pass one. So let's say the parameter is 20 and we'll run, it says good morning and now Let's make this 80 run and now it tells us good night. So this is how you lay on if else statement like I mentioned before, it's it's pretty simple. Um It's just piece of logic here. If it's true, do this, it's not true, do this instead. And of course you get more advanced with it. But this is just something that's pretty basic. But what if you have more than two conditions you want to run or you what if you have more than two conditions you want to check. So maybe good morning and good night is not enough, maybe you wanna get deeper into it. Um Like maybe if its uh noon you want to say hey it's lunchtime or if it's after tonight you can say what are you still doing here? Go to bed something like that. I don't know. Um So for that and else if wow that's very hey type this out rocks so ignore this. So none of this house saving an else if statement will help you out. And yes it is just like if else but for this one if you want more than two conditions it's called else if else if statements will help you out. Also statements allowed for more than two conditions to be checked. So let's go ahead head back to replicate and try that out. So I'm just gonna write this one from scratch is gonna be the same name that was self function declaration is going to be called greedy and still gonna pass the kindness of parameter. So for our first condition here or if. And I just realized since a day has 24 hours in it. Why why work off like less than 50 over greater than 50 let's do out of 24 Or we'll just use 24 as our like numbers parameter kind of thing. So this doesn't make any sense is it? Well it will only write it out. Sorry. So if time is Let's say less than 12. So we're going up like the hours in the day, 24 hours in the day. So if time is less than 12 council that log good morning. And then for the 2nd 1. So if it is instead of just writing else you're right. LCF just like the name of the thing, in the name of the agri conditional logic. So else if and then for this you will want to pass it a parameter. So for your else if condition how many of those you'll have, you will want to pass the parameter. So so else if time Is greater than Gosh, let's say six pm. So six pm would be 18 like 1800. So else if time is greater than 18 console dot log. Goodnight. Now we are just going to check three conditions here so we'll have an s if else if and then the last condition in your else if statement will just be else and just like else before we don't need to pass the parameter, it's just going to check everything that's not covered by this. So else so else's gonna be if it's less than 12 Or if it's greater than 12 But less than 18. So as you can see here so for the good morning, it's Less than 12 says good morning, it's greater than 18. Good night. So this is going to check between The 12 and 18. So for this statement we will console log good afternoon. So down here we will call upon our function And let's say it is eight a.m. Run. He says good morning. Let's say it is eight p.m. Or 20 100. Good night. Now let's check if it is 1400. So 14. Good afternoon. So there you are. So that is your introduction to conditional logic in javascript, using if else, statements and else if statements, so if you wanted to, you could write more else if statements here and then else again will just be the final one. Alright, everyone, thank you so much for tuning in that is it for this lesson and I will see you next time.
32 Views 0 Likes 0 Comments

Let's dive into some conditional logic with If Else statements!

Comment
Leave a comment (supports markdown format)