Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
At the heart of every conditional statement is the condition -- a true or false comparison that determines which code the program should run. Learn the different ways JavaScript lets you compare two values.
-
0:00
In the last video,
-
0:01
you learned about an important programming concept, the conditional statement.
-
0:06
Programmers use conditional statements to control the flow of their programs and
-
0:11
make them respond differently based on conditions inside the program.
-
0:15
The most important part of the statement is the condition itself.
-
0:19
It's a test that checks to see if something is either true or false.
-
0:25
For example, in this code the condition is spaceShips equal zero,
-
0:29
which breaks down to a simple question.
-
0:32
Is the value stored in the variable spaceShips equal to zero?
-
0:36
The answer is either yes or no.
-
0:40
If the answer is yes, the game is over and an alert dialogue appears.
-
0:45
As it turns out, all conditions, no matter how complex, end up with a simple yes or
-
0:50
no answer or, as computers like to think of it, true and false.
-
0:58
JavaScript provides lots of ways to test things.
-
1:01
We've seen the triple equals operator,
-
1:03
which is a way to test the equality of two things.
-
1:06
You can use that operator to see if the value a user
-
1:10
typed matches a particular string.
-
1:12
Or if the value in a variable is a particular number.
-
1:16
JavaScript provides other comparison operators to
-
1:18
help with conditional statements.
-
1:21
The greater than and less than symbols are great for comparing numeric values.
-
1:26
For example, you can make a game harder if the player's score is greater than 1,000.
-
1:31
Or, on the other hand, if a visitor is less than 18 years old you could
-
1:35
tell him to come back when he's older.
-
1:37
To get some practice understanding conditions let's look at
-
1:40
a few examples on the screen and you decide if the condition is true or false.
-
1:45
Three greater than two, this is true because three is a larger number than two.
-
1:50
100 greater than 100.
-
1:53
This is false because the two values are the same.
-
1:57
100 isn't greater than itself.
-
1:59
How about this.100 greater than or equal to 100.
-
2:02
This is true, because although the value on
-
2:05
the left isn't greater than the value on the right, it is equal to it.
-
2:10
Negative 12 less than zero.
-
2:12
This is true.
-
2:14
Because negative numbers are smaller than positive numbers or zero.
-
2:18
Okay, one more.
-
2:20
Apple less than bear.
-
2:22
The greater than and less than operators aren't limited to just numbers.
-
2:26
In this case, the comparison is true.
-
2:28
Because the first letter of the string on the left, the A in apple,
-
2:33
comes before the first letter of the second string, the B in bear.
-
2:38
That is A comes before B in the alphabet.
-
2:41
Letter strings will always come after number strings too.
-
2:45
That is if you compared a number and a letter, a number is less than a letter.
-
2:50
JavaScript provides two ways to test if values are equal.
-
2:55
The double equal sign is called the equality operator.
-
2:58
It lets you test if two values are the same, but it makes some allowances for
-
3:03
different types of values.
-
3:05
For example, three inside of quotes is a string.
-
3:10
And three without quotes is a number.
-
3:12
But if you use the double equal sign than the browser actually
-
3:15
converts the string to a number before comparing them, like this.
-
3:21
This comparison is true,
-
3:22
because the JavaScript interpreter converts the string to the number three.
-
3:27
And says that, yes, the number three is the same as the second number three.
-
3:32
The triple equal sign is called a strict equality operator.
-
3:36
It compares the type as well as the value.
-
3:40
In this case, the comparison is false, because one value is a string, and
-
3:45
the other is a number.
-
3:46
They aren't the same type of value, so they aren't equal.
-
3:50
It might seem like using the double equals operator is better,
-
3:53
since it seems more flexible.
-
3:55
However, professional JavaScript programmers tend to
-
3:58
use the triple equals operator.
-
4:00
And avoid the double equals.
-
4:03
Why? Because you
-
4:04
can get into some weird situations when using just the double equals operator.
-
4:08
Here's one example.
-
4:10
This is true.
-
4:11
Huh?
-
4:12
An empty string is equal to zero?
-
4:14
But if you use the strict equality operator the result is false.
-
4:19
What you'd expect.
-
4:21
In many cases the two equality operators work the same, but
-
4:24
there are a few cases like the one I just showed you where
-
4:27
the doubled equal sign will get you into a lot of trouble.
-
4:31
In this course I'll be using the strict or triple equals operator.
-
4:36
Let's look at a few examples using the strict equality operator.
-
4:39
This comparison is false.
-
4:41
Because the two numbers aren't equal.
-
4:44
This one is also false because those strings have different letters in them.
-
4:49
Whereas this one is true because those strings are the same.
-
4:54
But remember that java script is case sensitive so, this comparison is
-
4:58
false because the first string has all lower case and the second doesn't.
-
5:04
A lower case j isn't equal to an upper case J.
-
5:07
JavaScript also provides a way to test whether two values are not equal.
-
5:13
There's the basic not equal operator.
-
5:15
It's an exclamation mark followed by an equals sign.
-
5:19
The exclamation point is called a not operator so
-
5:23
exclamation equals is not equal to.
-
5:26
It's better to use the strict not equals.
-
5:29
That exclamation equals equals, for
-
5:32
the same reasons it's better to use the strict equality comparison operator.
-
5:37
Let's look at a few examples of the strict not equals operator.
-
5:41
This comparison is true because ten isn't equal to nine.
-
5:46
This is also true because the value on the left is a string and
-
5:49
the value on the right is a number.
-
5:51
So the types don't match.
-
5:54
This comparison is also true because the upper case R in
-
5:57
the first string isn't the same as the lower case r in the second string.
-
6:02
This comparison is false because the two numbers are the same.
You need to sign up for Treehouse in order to download course files.
Sign up