Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed JavaScript Basics!
      
    
You have completed JavaScript Basics!
Preview
    
      
  The most important part of a conditional statement is the condition itself. It's a test that checks to see if something is either true or false. JavaScript provides "comparison operators" to help with conditional statements.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      In the previous video, you learned
about an important programming concept,
                      0:00
                    
                    
                      the conditional statement.
                      0:04
                    
                    
                      You used the conditional statement
to control the flow of a program.
                      0:05
                    
                    
                      It made decisions and carried out actions
accordingly depending on different inputs.
                      0:08
                    
                    
                      The most important part of the conditional
statement is the condition itself.
                      0:14
                    
                    
                      It's a test that checks to see if
something is either true or false.
                      0:18
                    
                    
                      For example, in this code, the condition
is spaceShips === 0, which breaks down
                      0:22
                    
                    
                      to a simple question, is the value stored
in the variable spaceShips equal to zero?
                      0:27
                    
                    
                      The answer is either yes or no.
                      0:32
                    
                    
                      If the answer is yes, the game is over and
an alert dialog appears.
                      0:35
                    
                    
                      As it turns out, all conditions no matter
how complex, end up with a simple yes or
                      0:40
                    
                    
                      no answer, or as computers like
to think of it, true or false.
                      0:46
                    
                    
                      JavaScript provides lots
of ways to test things.
                      0:50
                    
                    
                      We've seen the triple equals operator,
                      0:54
                    
                    
                      which is a way to test
the equality of two things.
                      0:56
                    
                    
                      You can use that operator to see if the
value a user typed matches a particular
                      0:59
                    
                    
                      string or if the value in
a variable is a particular number.
                      1:03
                    
                    
                      JavaScript provides other
comparison operators to help with
                      1:07
                    
                    
                      conditional statements.
                      1:10
                    
                    
                      Some of them you might
already know from math.
                      1:12
                    
                    
                      For example, the greater than and
less than symbols are often used for
                      1:14
                    
                    
                      comparing numeric values.
                      1:18
                    
                    
                      For instance,
                      1:19
                    
                    
                      you can make a game harder if
the player's score is greater than 1000.
                      1:20
                    
                    
                      Or on the other hand,
if a visitor is less then a certain age,
                      1:24
                    
                    
                      you can tell them to come
back when they are older.
                      1:27
                    
                    
                      To get some practice understanding
conditions, let's look at a few examples.
                      1:30
                    
                    
                      In the JavaScript console, I'll start by
typing a few conditions greater than and
                      1:35
                    
                    
                      less then operators and you try to
decide if a condition is true or false.
                      1:40
                    
                    
                      First, 3 is greater than 2, this is true,
because 3 is a larger number than 2.
                      1:44
                    
                    
                      Next, 100 is greater than 100,
this is false,
                      1:52
                    
                    
                      because the two values are the same,
100 isn't greater than itself.
                      1:56
                    
                    
                      How about 100 is greater than or
equal to 100?
                      2:02
                    
                    
                      Well, this is true, because although
the value on the left isn't
                      2:07
                    
                    
                      greater than the value on the right,
it's still equal to it.
                      2:11
                    
                    
                      Here's another one,
negative 12 is less than 0.
                      2:15
                    
                    
                      And this is true because negative numbers
are smaller than positive numbers or zero.
                      2:20
                    
                    
                      Okay, one more, the string apple
is less than the string bear.
                      2:25
                    
                    
                      The greater than and less than operators
aren't limited to just numbers.
                      2:33
                    
                    
                      In this case, the comparison is true
because the first letter of the string on
                      2:37
                    
                    
                      the left, the a in apple, comes before
the first letter of the second string,
                      2:42
                    
                    
                      the b in bear,
that is a comes before b in the alphabet.
                      2:48
                    
                    
                      Also in comparison checks, letter strings
always come after number strings.
                      2:51
                    
                    
                      For instance, if you check if
a number string like 100 is
                      2:56
                    
                    
                      less than a letter string like Apple,
                      3:01
                    
                    
                      a number is less than any letter,
so this returns true.
                      3:04
                    
                    
                      JavaScript provides two ways
to test if values are equal,
                      3:09
                    
                    
                      the double equal sign and
the triple equal sign.
                      3:13
                    
                    
                      The double equal sign is
called the equality operator.
                      3:16
                    
                    
                      It lets you test if two
values are the same, but
                      3:19
                    
                    
                      makes some allowances for
different types of values.
                      3:22
                    
                    
                      For example, 3 inside quotes is a string,
and 3 without quotes, it's a number.
                      3:26
                    
                    
                      But if you use the double equal sign,
                      3:33
                    
                    
                      then the browser converts the string
to a number before comparing them.
                      3:35
                    
                    
                      And this returns true,
                      3:39
                    
                    
                      because the JavaScript engine
converts the string to the number 3,
                      3:41
                    
                    
                      and says that yes, the number 3 is
the same as the second number 3.
                      3:46
                    
                    
                      The triple equal sign is called
a strict equality operator,
                      3:51
                    
                    
                      it compares the type as well as the value.
                      3:55
                    
                    
                      For example,
a strict comparison like this is false,
                      3:58
                    
                    
                      because one value is a string and
the other is a number.
                      4:02
                    
                    
                      They are not the same type of value,
so they are not equal.
                      4:05
                    
                    
                      It might seem like using the double
equals operator is better since it's more
                      4:09
                    
                    
                      flexible.
                      4:13
                    
                    
                      However, JavaScript developers tend
to use the triple equals operator and
                      4:14
                    
                    
                      avoid the double equals, why?
                      4:18
                    
                    
                      Well, you can get into some weird
situations using the double equals
                      4:21
                    
                    
                      operator.
                      4:24
                    
                    
                      Here's one example,
an empty string is equal to 0.
                      4:24
                    
                    
                      This returns true, an empty string
is equal 0, strange isn't it?
                      4:30
                    
                    
                      And you can learn why this happens in
the teacher's notes with this video.
                      4:35
                    
                    
                      But notice, that if you use a strict
equality operator, the result is false.
                      4:38
                    
                    
                      In many cases, the two equality
operators work the same, but
                      4:44
                    
                    
                      there are a few cases,
like the one I just showed you,
                      4:47
                    
                    
                      where the double equal sign
might get you in trouble.
                      4:50
                    
                    
                      So for that reason, I'll use strict
equality or the triple equals operator for
                      4:53
                    
                    
                      the rest of this course.
                      4:58
                    
                    
                      All right, now,
                      4:59
                    
                    
                      let's look at a few examples using
the strict equality operator.
                      5:00
                    
                    
                      165 === 165.9, this is false,
                      5:04
                    
                    
                      because the two numbers are not equal.
                      5:09
                    
                    
                      How about the ' Python ' === the " HTML '?
                      5:14
                    
                    
                      This is also false because those
strings have different letters in them.
                      5:21
                    
                    
                      Whereas ' JavaScript ' ===
' JavaScript ' is true,
                      5:29
                    
                    
                      because those strings
are exactly the same.
                      5:34
                    
                    
                      But remember that JavaScript
is case sensitive.
                      5:38
                    
                    
                      So comparing Javascript in all lowercase,
                      5:41
                    
                    
                      with JavaScript as it's
normally written is false,
                      5:44
                    
                    
                      because the first string has all lowercase
letters, and the second does not.
                      5:48
                    
                    
                      A lowercase j isn't
equal to an uppercase J.
                      5:54
                    
                    
                      JavaScript also provides ways to test
whether two values are not equal.
                      5:59
                    
                    
                      There's the not equal or
inequality operator.
                      6:03
                    
                    
                      It's an exclamation mark
followed by an equal sign.
                      6:06
                    
                    
                      The exclamation point is called a logical
NOT operator, so != means not equal to.
                      6:09
                    
                    
                      But keep in mind that it's better to use
the strict inequality operator with two
                      6:15
                    
                    
                      equal signs, for
                      6:19
                    
                    
                      the same reasons, it's better to use
the strict equality comparison operator.
                      6:20
                    
                    
                      So let's look at a few examples of
the strict inequality operator.
                      6:24
                    
                    
                      First, 10 !== 9,
                      6:28
                    
                    
                      this is true because 10 is not equal to 9.
                      6:32
                    
                    
                      How about ' 10 ' !== 10?
                      6:37
                    
                    
                      This is also true because the value
on the left is a string, and
                      6:42
                    
                    
                      the value on the right is a number,
so the two types do not match.
                      6:46
                    
                    
                      Let's check if 'Java' !==
                      6:50
                    
                    
                      'java' in all lowercase.
                      6:55
                    
                    
                      And this is true,
because the uppercase J in the first
                      6:59
                    
                    
                      string isn't the same as
the lowercase j in the second string.
                      7:03
                    
                    
                      Finally, negative 59 !== negative 59,
well,
                      7:08
                    
                    
                      this is false,
because the two numbers are the same.
                      7:13
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up