1 00:00:00,510 --> 00:00:04,077 In the previous video, you learned about an important programming concept, 2 00:00:04,077 --> 00:00:05,436 the conditional statement. 3 00:00:05,436 --> 00:00:08,906 You used the conditional statement to control the flow of a program. 4 00:00:08,906 --> 00:00:14,115 It made decisions and carried out actions accordingly depending on different inputs. 5 00:00:14,115 --> 00:00:18,157 The most important part of the conditional statement is the condition itself. 6 00:00:18,157 --> 00:00:22,152 It's a test that checks to see if something is either true or false. 7 00:00:22,152 --> 00:00:27,343 For example, in this code, the condition is spaceShips === 0, which breaks down 8 00:00:27,343 --> 00:00:32,409 to a simple question, is the value stored in the variable spaceShips equal to zero? 9 00:00:32,409 --> 00:00:35,427 The answer is either yes or no. 10 00:00:35,427 --> 00:00:40,619 If the answer is yes, the game is over and an alert dialog appears. 11 00:00:40,619 --> 00:00:46,349 As it turns out, all conditions no matter how complex, end up with a simple yes or 12 00:00:46,349 --> 00:00:50,832 no answer, or as computers like to think of it, true or false. 13 00:00:50,832 --> 00:00:54,022 JavaScript provides lots of ways to test things. 14 00:00:54,022 --> 00:00:56,343 We've seen the triple equals operator, 15 00:00:56,343 --> 00:00:59,071 which is a way to test the equality of two things. 16 00:00:59,071 --> 00:01:03,844 You can use that operator to see if the value a user typed matches a particular 17 00:01:03,844 --> 00:01:07,585 string or if the value in a variable is a particular number. 18 00:01:07,585 --> 00:01:10,788 JavaScript provides other comparison operators to help with 19 00:01:10,788 --> 00:01:12,212 conditional statements. 20 00:01:12,212 --> 00:01:14,642 Some of them you might already know from math. 21 00:01:14,642 --> 00:01:18,300 For example, the greater than and less than symbols are often used for 22 00:01:18,300 --> 00:01:19,791 comparing numeric values. 23 00:01:19,791 --> 00:01:20,616 For instance, 24 00:01:20,616 --> 00:01:24,619 you can make a game harder if the player's score is greater than 1000. 25 00:01:24,619 --> 00:01:27,934 Or on the other hand, if a visitor is less then a certain age, 26 00:01:27,934 --> 00:01:30,676 you can tell them to come back when they are older. 27 00:01:30,676 --> 00:01:35,664 To get some practice understanding conditions, let's look at a few examples. 28 00:01:35,664 --> 00:01:40,595 In the JavaScript console, I'll start by typing a few conditions greater than and 29 00:01:40,595 --> 00:01:44,908 less then operators and you try to decide if a condition is true or false. 30 00:01:44,908 --> 00:01:52,326 First, 3 is greater than 2, this is true, because 3 is a larger number than 2. 31 00:01:52,326 --> 00:01:56,696 Next, 100 is greater than 100, this is false, 32 00:01:56,696 --> 00:02:02,589 because the two values are the same, 100 isn't greater than itself. 33 00:02:02,589 --> 00:02:07,676 How about 100 is greater than or equal to 100? 34 00:02:07,676 --> 00:02:11,566 Well, this is true, because although the value on the left isn't 35 00:02:11,566 --> 00:02:15,182 greater than the value on the right, it's still equal to it. 36 00:02:15,182 --> 00:02:20,263 Here's another one, negative 12 is less than 0. 37 00:02:20,263 --> 00:02:25,753 And this is true because negative numbers are smaller than positive numbers or zero. 38 00:02:25,753 --> 00:02:33,992 Okay, one more, the string apple is less than the string bear. 39 00:02:33,992 --> 00:02:37,979 The greater than and less than operators aren't limited to just numbers. 40 00:02:37,979 --> 00:02:42,985 In this case, the comparison is true because the first letter of the string on 41 00:02:42,985 --> 00:02:48,067 the left, the a in apple, comes before the first letter of the second string, 42 00:02:48,067 --> 00:02:51,631 the b in bear, that is a comes before b in the alphabet. 43 00:02:51,631 --> 00:02:56,768 Also in comparison checks, letter strings always come after number strings. 44 00:02:56,768 --> 00:03:01,742 For instance, if you check if a number string like 100 is 45 00:03:01,742 --> 00:03:04,926 less than a letter string like Apple, 46 00:03:04,926 --> 00:03:09,619 a number is less than any letter, so this returns true. 47 00:03:09,619 --> 00:03:13,234 JavaScript provides two ways to test if values are equal, 48 00:03:13,234 --> 00:03:16,277 the double equal sign and the triple equal sign. 49 00:03:16,277 --> 00:03:19,283 The double equal sign is called the equality operator. 50 00:03:19,283 --> 00:03:22,715 It lets you test if two values are the same, but 51 00:03:22,715 --> 00:03:26,770 makes some allowances for different types of values. 52 00:03:26,770 --> 00:03:33,372 For example, 3 inside quotes is a string, and 3 without quotes, it's a number. 53 00:03:33,372 --> 00:03:35,593 But if you use the double equal sign, 54 00:03:35,593 --> 00:03:39,974 then the browser converts the string to a number before comparing them. 55 00:03:39,974 --> 00:03:41,628 And this returns true, 56 00:03:41,628 --> 00:03:46,423 because the JavaScript engine converts the string to the number 3, 57 00:03:46,423 --> 00:03:51,146 and says that yes, the number 3 is the same as the second number 3. 58 00:03:51,146 --> 00:03:55,585 The triple equal sign is called a strict equality operator, 59 00:03:55,585 --> 00:03:58,640 it compares the type as well as the value. 60 00:03:58,640 --> 00:04:02,274 For example, a strict comparison like this is false, 61 00:04:02,274 --> 00:04:05,915 because one value is a string and the other is a number. 62 00:04:05,915 --> 00:04:09,781 They are not the same type of value, so they are not equal. 63 00:04:09,781 --> 00:04:13,536 It might seem like using the double equals operator is better since it's more 64 00:04:13,536 --> 00:04:14,121 flexible. 65 00:04:14,121 --> 00:04:18,952 However, JavaScript developers tend to use the triple equals operator and 66 00:04:18,952 --> 00:04:21,000 avoid the double equals, why? 67 00:04:21,000 --> 00:04:24,251 Well, you can get into some weird situations using the double equals 68 00:04:24,251 --> 00:04:24,820 operator. 69 00:04:24,820 --> 00:04:30,105 Here's one example, an empty string is equal to 0. 70 00:04:30,105 --> 00:04:35,239 This returns true, an empty string is equal 0, strange isn't it? 71 00:04:35,239 --> 00:04:38,296 And you can learn why this happens in the teachers notes with this video. 72 00:04:38,296 --> 00:04:44,060 But notice, that if you use a strict equality operator, the result is false. 73 00:04:44,060 --> 00:04:47,419 In many cases, the two equality operators work the same, but 74 00:04:47,419 --> 00:04:50,325 there are a few cases, like the one I just showed you, 75 00:04:50,325 --> 00:04:53,251 where the double equal sign might get you in trouble. 76 00:04:53,251 --> 00:04:58,042 So for that reason, I'll use strict equality or the triple equals operator for 77 00:04:58,042 --> 00:04:59,556 the rest of this course. 78 00:04:59,556 --> 00:05:00,636 All right, now, 79 00:05:00,636 --> 00:05:04,675 let's look at a few examples using the strict equality operator. 80 00:05:04,675 --> 00:05:09,957 165 === 165.9, this is false, 81 00:05:09,957 --> 00:05:14,675 because the two numbers are not equal. 82 00:05:14,675 --> 00:05:21,895 How about the ' Python ' === the " HTML '? 83 00:05:21,895 --> 00:05:25,745 This is also false because those strings have different letters in them. 84 00:05:29,109 --> 00:05:34,486 Whereas ' JavaScript ' === ' JavaScript ' is true, 85 00:05:34,486 --> 00:05:38,670 because those strings are exactly the same. 86 00:05:38,670 --> 00:05:41,226 But remember that JavaScript is case sensitive. 87 00:05:41,226 --> 00:05:44,549 So comparing Javascript in all lowercase, 88 00:05:44,549 --> 00:05:48,590 with JavaScript as it's normally written is false, 89 00:05:48,590 --> 00:05:54,537 because the first string has all lowercase letters, and the second does not. 90 00:05:54,537 --> 00:05:59,101 A lowercase j isn't equal to an uppercase J. 91 00:05:59,101 --> 00:06:03,780 JavaScript also provides ways to test whether two values are not equal. 92 00:06:03,780 --> 00:06:06,429 There's the not equal or inequality operator. 93 00:06:06,429 --> 00:06:09,330 It's an exclamation mark followed by an equal sign. 94 00:06:09,330 --> 00:06:15,584 The exclamation point is called a logical NOT operator, so != means not equal to. 95 00:06:15,584 --> 00:06:19,671 But keep in mind that it's better to use the strict inequality operator with two 96 00:06:19,671 --> 00:06:20,586 equal signs, for 97 00:06:20,586 --> 00:06:24,808 the same reasons, it's better to use the strict equality comparison operator. 98 00:06:24,808 --> 00:06:28,935 So let's look at a few examples of the strict inequality operator. 99 00:06:28,935 --> 00:06:32,077 First, 10 !== 9, 100 00:06:32,077 --> 00:06:37,622 this is true because 10 is not equal to 9. 101 00:06:37,622 --> 00:06:42,819 How about ' 10 ' !== 10? 102 00:06:42,819 --> 00:06:46,784 This is also true because the value on the left is a string, and 103 00:06:46,784 --> 00:06:50,919 the value on the right is a number, so the two types do not match. 104 00:06:50,919 --> 00:06:55,980 Let's check if ' Java ' !== ' 105 00:06:55,980 --> 00:06:59,825 java ' in all lowercase. 106 00:06:59,825 --> 00:07:03,582 And this is true, because the uppercase J in the first 107 00:07:03,582 --> 00:07:08,021 string isn't the same as the lowercase j in the second string. 108 00:07:08,021 --> 00:07:13,436 Finally, negative 59 !== negative 59, well, 109 00:07:13,436 --> 00:07:18,630 this is false, because the two numbers are the same.