Check out a free preview of the full Introduction to JavaScript course:
The "Control Flow" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson's course:

Brian introduces the concept of control flow, the order in which individual statements are evaluated.

Get Unlimited Access Now

Transcript from the "Control Flow" Lesson

>> Brian: So, next we're going to get into what's called, controlled flow. So, we're gonna go back to our sky is blue example. Const sky is blue.
>> Brian: Let's just do, is sky blue.
>> Brian: So again, naming convention, I like to make boolean questions, because it's either a yes or no answer, right?

So is sky blue, by reading that, you can look at that, and without ever having seen the code where is sky blue is defined, that's gonna be a boolean, right? Because it's asking you a question, to which there is a yes and a no answer.
>> Brian: So.
>> Brian: Here we're gonna say, if is sky blue.

>> Brian: We're gonna say console.log the sky is blue, else, console.log. The sky is not blue? Question. [LAUGH]
>> Brian: This is called an if statement, intuitively, I would hope. [LAUGH] So you're gonna say if, and then between these parentheses here, you're going to ask some sort of question, right?

That's going to be answerable either by a yes or no. What's inside of your house to evaluate to a boolean, right? It has to be the true or has to be false. It's in some capacity, right? So in this particular case, I've already done statically defined above, that sky is blue, is going to be true.

So in this case, this block is always going to be executed, right? And this will never be executed because, I've defined is sky blue up here to be true. So if you go over here and refresh, you can see it says, here the sky is blue because, this is being evaluated to true.

>> Brian: So, if this is true, it's going to execute what's ever in this block, right? When I say block, I mean, whatever is between these particular curly braces. Right? So I can have multiple things in here.
>> Brian: Right? And then this block ends whenever this curly brace finishes, right, when you ever you close that curly brace.

>> Brian: And actually, you don't even need the else, the else part is not required.
>> Brian: But as you may imagine,
>> Brian: If this is not true, if this is false, right, it's going to evaluate whatever is in the else block. So just to demonstrate that, I'm gonna change this to be false.

And now what would you expect to happen? Sky is not blue.
>> Brian: Is called conditional or if statements, right? This helps you do conditional logic. So, maybe you might have something, if the temperature is above 80 today then you say, it's a nice day. If it's below 80, then it's not a nice day.

I don't know, I'm making things up. I'm very picky about my weather. Says the person who lives in Seattle. You can even do fun things like this. If we say let
>> Brian: Greeting, right, I don't know.
>> Brian: And then here I can say greeting,
>> Brian: Equals.
>> Brian: It must be nice weather.

>> Brian: And then here inside of the else block I can say, greeting, bad weather. Right, cuz if the sky's not blue today. And down here I can say, console.log greeting.
>> Brian: So, since today the sky here is grey, what would you expect to be logged out here on line 11?

>> Brian: It must be bad weather, right? Because is sky blue is false, so when it asks this question, if sky is blue, then it's gonna hit the else block and says, it must be bad weather, right? And so I can conditionally define how these are done. I'll talk about in a second, why I defined greeting outside of the if statements, for now, roll with me that that's why I had to do it that way.

>> Speaker 2: And you had to use let, couldn't use const.
>> Brian: Right. Correct.