Loading
Lesson 30
Courses / JavaScript for Complete Beginners
Using if, “else if”, else Statement in JavaScript

Video Transcript

Welcome to MDK Tech World. We have learned about the IF-ELS statement. In this lesson, we're going to go further and learn about yet another way of figuring out where to go in the flow of your program. In the IF-ELS statement, we learned that you can control the flow to go one way or another. Now we're going to learn to do just that except we can go not only one of the ways, but more than two ways. We're going to learn about the IF-ELS statement. So as an example, look at what we learned. Let's look at this diagram here. So suppose you are at a crossroads. You have two ways of going. This is the road and you can go this way to the left, or you can go that way to the right. Now this perfectly illustrates the IF-ELS statement. If a certain condition is true, you go this way down to the left. Otherwise, else, you go the other way. Now what if we want it to actually, the roads had not only two ways to go, but an extra way. For example, if this guy extended like that, now we have three ways we can go, right? We can go bottom left, strictly down, or to the right. Now how would we do this? That's what we're going to learn. We're going to learn about the IF-ELS statement. Let's look at some code. So I'm going to open the text editor here. And this is what we learned so far. Here I have my text editor. I am using Adam. And it's colored differently here because there's syntax highlighting. That's when certain keywords in the language, in this case the JavaScript language, have some special meaning and they highlight with a different color. Now we learned this construct before, right? So if openTorrentis is some condition here, some expression that yields a Boolean variable, value, that's true or false, right? Then we're going to execute this piece of code in between the IF block, in between these curly braces. By the way, these slash slash whatever here, it's called a comment. Commenting after a slash slash is taken as a comment by the JavaScript language and this will not get executed at all. So I just use this comment to illustrate, to make a comment that do this, whatever is within these curly braces. So if this condition turns out to be false, it's not going to execute the IF statement block, it's going to execute the else part, this guy right here. If this condition turns out to be false, it's going to execute the else block. Everything in between the opening and closing curly brace for the else clause will be executed. Again I use slash slash, otherwise do this just to make a comment here. There will be some code right here. We would do whatever, console log, etc, etc. I just want to illustrate that this part here, whatever you write here, will be executed if the condition is false. So that's the IF else, we already know that, right? Now we introduced the third path in the road in our diagram, remember? So how do we do that? Let's look at some code. Here I have another file, illustrating the else if part. So not only now do we have IF and else, we have something in between and that's called the else if. This else if right here will be an in-between condition that will be checked before you get to the else case. So let's start from the beginning. So you start out saying if certain condition is going to be checked, right? Now if this condition is true, we're all good, we're going to execute everything in between the IF clause block. Now everything else will be skipped, will not be executed. Now we understand that. Now if this condition is false, what's going to happen is the following. It's going to go to the next statement which turns out to be an else if. Now else if allows you to check yet another condition. So you can put another condition here to see if that turns out to be true. We know this if condition here was false. Then it's going to go to the else if and check this other condition. If this condition turns out to be true, it's going to execute this else if block whatever code you put in between these curly braces. Everything else will be ignored, okay? So only this part will be executed. Now if the condition of the else if is false, then it's going to go finally to the else statement and execute whatever you placed in between the curly braces. Okay? Just like the normal IF, ELSE, FLOW you learned before. So the difference now is you can check for extra condition. An extra condition here with this else if is trying to go straight to the end of the last resort case with the else. So that's the else if. Let's illustrate that with an example. Let's jump to code. I'm going back to the browser to the browser console. I'm using Firefox by the way. So let's say you have some variable. Let's start some letter for example, letter B. So I'm going to start a B. It's a string. Letter B. Let me use that by the way in case I need to change it later. So this variable called letter stores the letter B. Let's make an IF statement to check if the letter is A, B, or any other letter. So to start out, IF the letter is A, we're going to do a console log. I haven't put the condition so I want you to try with me. Let me just write out what we want to accomplish with each case. So in the first case, the letter is A. Now the second case, we're going to do an else if. We're going to say the letter is B. And then the other case, in case any other letter, the letter is not A, nor is it B. It could be C, D, E, F, G, whatever up to Z. Whatever character. Now these are the cases. Now for the first one, we want to check if the letter is A. What's the condition here? So the variable is letter and you want to check if the value is A. A is a string. So we're going to say letter. We're going to use the triple equals, open quotes, A. So if the letter is A, we're going to print to the console the letters A. Now I want to see you do the else if condition here, there. So how did it go? We're going to have letter, triple equals what? B. So it's going to check the value of this variable to see if it's strictly equal to the letter B, the string with the character B. Upper case, by the way. All right? It's case sensitive, so be sure to be typing upper case. Otherwise if the letter is not A, it's not B, then it's got to be something else. We're going to do this console lot. Okay? Notice that it's checking first this case is the letter A if so execute this guy. Otherwise go to the next else if check the condition. Okay, is the letter B? If so, execute this. Otherwise if this condition is false, execute the else. Let's try it out. So the letter is B right now. So what would you expect to be console lot? You would expect the else if, right? There you go. The letter is B. Now let's try letter being A. Let me redefine the variable letter to be A. Then I'm going to do the same thing we typed before. I use the up arrow in my keyboard to go back to my previous statements that I typed before. Let's try it again. Now the letter is A. Okay? Because I changed the letter to A, it went into the if statement. Check the condition. Letter in this case is A. So this is going to be true. Execute the if block statements and everything else is skipped, is ignored and will not be executed. Okay? So that's it for the else if statement. We have if, else if, else. Okay? That is it for this lesson and I'll see you on the next one.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: