if

Here's a good memory tip for learning what conditionals means. When you were young and you asked your parents to do something, they'd say: "If you clean your room, then you can drive the Ferrari to Florida for Spring Break." Ok, maybe it wasn't that good, but you get the idea. If you met a certain condition, then something else could happen.

In code we have conditions, and we call these expressions conditionals. In English we have words called subordinate conjunctions, if, when, while and many others. In code we have keywords that mark the start of a conditional phrase. As you may have already deduced, if is one of those keywords.

Sample Code

Let's look at some conditionals. Just a quick review here. Think of what will happen. Consider that will happen with the following code:

var roomClean = true;

if(roomClean) {
    console.log("I can take the Ferrari out!");
}

A little more discussion

  1. The above statement is called an if statement.

  2. It will only execute if roomClean is true.

  3. If roomClean isn't true, it will simply skip over the code inside the if code block.

Practice

Timed practice, in if.js:

  1. See if you can write 5 if statements in about 5 minutes.

  2. You should have 5 variables at the top of the file and 5 if statements underneath those variables.

  3. Make it fun. Think of movies, friends, or food in your life that you could write an if statement for.

Last updated