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