FLASH SALE! Up to 40% off on unlimited courses, tutorials and creative asset downloads SAVE NOW
by
FREELessons:18Length:1.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.6 If/Else Statements

Let’s now look at the if/else statement. If you know any other programming language then you’ll already be familiar with the concept of conditional statements. If not, don’t worry—we’ll cover all of that in this lesson.

2.6 If/Else Statements

Hello and welcome to lesson number seven where you'll learn about the if else statement. Now and this is something essential for any kind of programmer. And the if else statement allows you to execute various blocks of code if a specific condition is met. So first let's have a look at the if statement. So the syntax is very simple if we open some parentheses and in here we put a condition. So let's begin by declaring a variable x = 5. And it can say, I can say if x = 5 then we're going to do a console.log True. And if I do a refresh I get across a lot of True. Now what happens here is first JavaScript evaluates this expression is x = 5? Yes it is. If true then we do the console.log. So it's the same thing as saying if true, It's gonna have the same result. Or if 1, 1 is also interpreted as true. But if I say if x = 51 for example nothing's gonna happen because this condition is gonna be evaluated, it's gonna be evaluated to false actually, and the code inside the curly brackets is not gonna be run. Well in that case we can specify else. So if this condition is not met then we do this. And I'm going to say console.log False. And you can actually chain these together, right? Let's make a better example here. Let's say age, var age = 24, right? So we're gonna say if age is smaller than let's say 20, we're gonna log to the console Teenager. Else if age is higher or equal than 20 and age equals smaller than 70 then we're going to log a young man. Else we're gonna log not so young anymore. So let's see what happens. If I do a refresh it tells me I'm a young man. But if I change my age here to, for example 70, it tells me that I'm not so young anymore because 70 doesn't actually fall in these two categories. If I would say smaller or equal than 70 it's gonna still say I'm a young man. So this is how the if else statement works. Now finally I want to show you another operator that's called the ternary operator, or a conditional operator, and that goes something like this. This is actually a special case because it's the only JavaScript operator that takes three operates. And let's do a simple if else. I wanna say if age is higher than 20 and then I'm gonna console.log Not a teen anymore, and I'm gonna do else console.log Teen. And I'll just comment this bit, okay. So now it tells me not a teen anymore. Well this I can actually compress to this, age > 20 question mark console.log Not a teen anymore : console.log Teen. And let's see, it does exactly the same thing and this is the way it works, the expression here, or the condition, is evaluated. And if it's true, we're gonna execute the code after the question mark. If it's not true, we're gonna execute this code. So a more simple version would be this, condition val1, val2. So if this is true, execute this, otherwise, execute this. And that's how you can use the if else statement. Now let's move on and talk about loops. That's coming up next.

Back to the top
View on GitHub