Introduction to JavaScript

# Else If Statement

## Brian Holt

SQLite Cloud

### Check out a free preview of the full Introduction to JavaScript course

The "Else If Statement" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian codes an 'else if' statement into the 'if' statement, an additional condition for determining evaluated statements.

Preview
Close

### Transcript from the "Else If Statement" Lesson

[00:00:00]
>> Brian Holt: To just mess everything up, I just told you. [LAUGH] We're gonna we're gonna put it in a third condition here. So const friendsAtYourParty equals 10. So if friendsAtYourParty triple equals 0. Make this a bit bigger for now.
>> Brian Holt: Console.log, [LAUGH] Cool. Now I have all the nachos,

[00:00:38]
>> Brian Holt: To myself. I was definitely at a bar and hungry when I was writing this. [LAUGH]
>> Speaker 2: [LAUGH]
>> Brian Holt: That's not sad, right? [LAUGH]
>> Speaker 2: [LAUGH]
>> Brian Holt: Okay, then I'm gonna put an else if,
>> Brian Holt: FriendsAtYourParty is greater than or equal to 4.
>> Brian Holt: Console.log.
>> Brian Holt: Perfect amount to play some Mario Kart.

[00:01:20]
>> Brian Holt: Else,
>> Brian Holt: Console.log, Wooooooo, play the dance music.
>> Brian Holt: Okay? So let's dissect here a little bit. First of all, introduce you to these comparator operators, as they're called. Or you're just asking another question here, is friendsAtYourParty greater than or equal to 4? This is actually incorrect, I wanted this to be less than or equal to 4.

[00:02:03]
>> Brian Holt: So what's gonna happen here? So first, all this is asking the question, how many friends do you have at your party? If it's less than or equal to four, if this value is true, then do this, right?
>> Brian Holt: So the question you might ask yourself, if you have zero, right?

[00:02:25]
Both of these are true, right? This would be true and this would be true, right? But with if statements are guaranteed that only one of them. So if this one gets evaluated is true, it skips the rest of them, right? So it's never going to do multiple of them.

[00:02:41]
It's just going to find the first one of those that's true and then skip the rest of them, right? So if I refresh here, over here. If I have ten there then it says play the dance music. But if I have four there, what do you think it's gonna do?

[00:03:00]
>> Brian Holt: Perfect amount to play some Mario Kart, and if I have zero.
>> Brian Holt: Then you have all the nachos to yourself, or if I have one,
>> Brian Holt: Or five.
>> Brian Holt: So that's what these, else if statements are useful for. It will just go down and you'll keep asking questions until it either hits an else block.

[00:03:30]
And if it gets to an else block then it just does that, right? Or if none of them are true and you don't have an else block that just skips all of them.
>> Brian Holt: So before actually, in my note, I'm noticing here I actually have this being greater or equal to four that is a bug, right?

[00:03:47]
Cuz now I have this kind of strange condition where if I put this to be three, it's gonna say play the dance music, right? Because three is not greater than or equal to four, and so then it's going to hit this else block, right? But then if I put this as five, that's gonna say Mario Kart because it is greater than or equal to four.

[00:04:14]
So that would be a bug. [LAUGH]
>> Brian Holt: Every time that I have to do comparisons, I have to sit like stop and sit there for a while, I was like wait, what is this actually trying to do?. [LAUGH].
>> Brian Holt: Math is hard. So those are conditionals, if statements.

[00:04:37]
You use these all the time in JavaScript, and in programming in general.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses