JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • Sample Code
  • A little more discussion
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 2-Control Flow and Error Handling

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.

Previous2-Control Flow and Error HandlingNextif else

Last updated 7 years ago