This course has been updated! We now recommend you take the Getting Started with JavaScript, v2 course.

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

One major use for block statements is with conditions. An “if” statement checks a condition and will execute the code within its block if the condition is true. If the condition checked is only a value, the value will be converted to a Boolean.

Get Unlimited Access Now

Transcript from the "Conditional Statements" Lesson

[00:00:00]
>> Kyle Simpson: We mentioned that there was a kind of block called a conditional in if statement. Let's focus more specifically on a conditional. If I have the var [COUGH] a is equal to 10, and I wanna do something only if a is greater than 5, then I put an if statement around a block.

[00:00:20] And here I say a is equal to a times 2. So I'm only doubling the a variable. I'm only doubling its value if it was already greater than 5, for whatever reason. The if statement says, take this thing, whatever this expression is, and figure out if it's true or false.

[00:00:44]
>> Kyle Simpson: But there are statement expressions that you can put there which are not already true or false. This one is, the greater than operator always says that's either a true or false. So the result of that operator is always either true or false. But there are certain expressions that we could put here that would not result in true or false by themselves.

[00:01:09] For example, that expression. Which is extremely common in JavaScript, people put in expression like if (a). The if statement still expects a true or a false. And if you don't pass a true or a false, guess what has to happen?
>> Speaker 2: [INAUDIBLE]
>> Kyle Simpson: It has to convert. Remember we talked about type conversion before?

[00:01:33] And I showed you type conversion with numbers and strings? Well, there's also type conversion between any value and what's called a boolean value, that is the true or the false value. So this variable gets converted either to a true value or a false value. And the rules for how that conversion happens are fairly straightforward.

[00:01:55] There's a list of values defined in the JavaScript spec, which are called falsy values. Falsy, F-A-L-S-Y, okay? So falsy values are defined as these. 0, negative 0, NaN, which is the not a number number, empty string, false itself, null, and undefined. That list of values is the list of falsy values in JavaScript.

[00:02:34]
>> Kyle Simpson: And the easiest thing for me to say is, the way you figure out if something is truthy, that is, that it's gonna become true, or it's falsy, it's gonna become false, is to just consult this list. Memorize the list and then just mentally consult it. It is either a falsy value because it's on this list, or the only other option is that it must be truthy, it must behave as a true value when coerced.

[00:03:00] So essentially, you look at the list, and if it's on the list, it's falsy, if it's not on the list, it must be truthy.
>> Kyle Simpson: So in our case, if that was our list, let's scroll down now. In our case, the question that we now ask is, let me scroll so I leave the list visible for you.

[00:03:21] In our case, is a truthy or falsey?
>> Speaker 3: Truthy cuz it exists.
>> Kyle Simpson: Why is it truthy?
>> Speaker 3: Cuz you assigned it above.
>> Kyle Simpson: Not because I assigned it above, but because of what value it currently has.
>> Speaker 3: Okay.
>> Kyle Simpson: Okay, the value in a is the value 10.

[00:03:42] Is the value 10 on the falsy list?
>> Speaker 3: No.
>> Kyle Simpson: So if it's not on the falsy list, it has to be conceptually on the truthy list. And all we need to know for now is that a falsy value becomes false when it has to be a boolean, and a truthy value becomes true.

[00:04:00] So if I said if (a), then a becomes true, and the if statement executes. Now what if a was 0, what would happen?
>> Speaker 2: [INAUDIBLE]
>> Kyle Simpson: The 0 value is a falsy value, so it would result in false. And the if statement would say, if false, nope, not gonna do it.

[00:04:21] So it makes a conditional decision based upon whether the expression you give it is true or false.
>> Speaker 3: So could a person say if a equal false?
>> Kyle Simpson: You could compare it directly to the true or the false value, like that or like that. I don't recommend it.

[00:04:39]
>> Speaker 3: Okay.
>> Kyle Simpson: There's lots of nuanced reasons why I don't recommend it. We won't go into those weeds. There's a question in the chat room about void 0. So void is an operator.
>> Kyle Simpson: It's not very commonly used, but it's an operator that everything that you give it after the void becomes undefined.

[00:05:05] So if I said void 42, if you try that expression in your browser, everybody try it, go to your console,
>> Kyle Simpson: Void 42 gives us undefined. So no matter what you pass to void, you get undefined back, which means void 0 also gives you undefined back.
>> Kyle Simpson: It's an operator, just like plus or minus or whatever.

[00:05:30] It's just an operator that voids out [COUGH] the value and leaves undefined in its place.