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
The above statement is called an
if statement.It will only execute if
roomCleanis true.If
roomCleanisn't true, it will simply skip over the code inside theifcode block.
Practice
Timed practice, in if.js:
See if you can write 5
ifstatements in about 5 minutes.You should have 5 variables at the top of the file and 5
ifstatements underneath those variables.Make it fun. Think of movies, friends, or food in your life that you could write an
if statementfor.
Last updated