1 00:00:00,640 --> 00:00:01,760 In the last video, 2 00:00:01,760 --> 00:00:06,648 you learned about an important programming concept, the conditional statement. 3 00:00:06,648 --> 00:00:11,000 Programmers use conditional statements to control the flow of their programs and 4 00:00:11,000 --> 00:00:15,880 make them respond differently based on conditions inside the program. 5 00:00:15,880 --> 00:00:19,890 The most important part of the statement is the condition itself. 6 00:00:19,890 --> 00:00:24,270 It's a test that checks to see if something is either true or false. 7 00:00:25,520 --> 00:00:29,320 For example, in this code the condition is spaceShips equal zero, 8 00:00:29,320 --> 00:00:32,990 which breaks down to a simple question. 9 00:00:32,990 --> 00:00:36,580 Is the value stored in the variable spaceShips equal to zero? 10 00:00:36,580 --> 00:00:40,330 The answer is either yes or no. 11 00:00:40,330 --> 00:00:45,260 If the answer is yes, the game is over and an alert dialogue appears. 12 00:00:45,260 --> 00:00:50,898 As it turns out, all conditions, no matter how complex, end up with a simple yes or 13 00:00:50,898 --> 00:00:58,010 no answer or, as computers like to think of it, true and false. 14 00:00:58,010 --> 00:01:01,300 JavaScript provides lots of ways to test things. 15 00:01:01,300 --> 00:01:03,810 We've seen the triple equals operator, 16 00:01:03,810 --> 00:01:06,950 which is a way to test the equality of two things. 17 00:01:06,950 --> 00:01:10,380 You can use that operator to see if the value a user 18 00:01:10,380 --> 00:01:12,860 typed matches a particular string. 19 00:01:12,860 --> 00:01:16,180 Or if the value in a variable is a particular number. 20 00:01:16,180 --> 00:01:18,920 JavaScript provides other comparison operators to 21 00:01:18,920 --> 00:01:21,350 help with conditional statements. 22 00:01:21,350 --> 00:01:26,260 The greater than and less than symbols are great for comparing numeric values. 23 00:01:26,260 --> 00:01:31,410 For example, you can make a game harder if the player's score is greater than 1,000. 24 00:01:31,410 --> 00:01:35,350 Or, on the other hand, if a visitor is less than 18 years old you could 25 00:01:35,350 --> 00:01:37,620 tell him to come back when he's older. 26 00:01:37,620 --> 00:01:40,410 To get some practice understanding conditions let's look at 27 00:01:40,410 --> 00:01:44,570 a few examples on the screen and you decide if the condition is true or false. 28 00:01:45,750 --> 00:01:50,750 Three greater than two, this is true because three is a larger number than two. 29 00:01:50,750 --> 00:01:53,470 100 greater than 100. 30 00:01:53,470 --> 00:01:57,440 This is false because the two values are the same. 31 00:01:57,440 --> 00:01:59,290 100 isn't greater than itself. 32 00:01:59,290 --> 00:02:02,990 How about this.100 greater than or equal to 100. 33 00:02:02,990 --> 00:02:05,710 This is true, because although the value on 34 00:02:05,710 --> 00:02:10,760 the left isn't greater than the value on the right, it is equal to it. 35 00:02:10,760 --> 00:02:12,400 Negative 12 less than zero. 36 00:02:12,400 --> 00:02:14,440 This is true. 37 00:02:14,440 --> 00:02:18,490 Because negative numbers are smaller than positive numbers or zero. 38 00:02:18,490 --> 00:02:20,330 Okay, one more. 39 00:02:20,330 --> 00:02:21,790 Apple less than bear. 40 00:02:22,820 --> 00:02:26,780 The greater than and less than operators aren't limited to just numbers. 41 00:02:26,780 --> 00:02:28,980 In this case, the comparison is true. 42 00:02:28,980 --> 00:02:33,600 Because the first letter of the string on the left, the A in apple, 43 00:02:33,600 --> 00:02:38,120 comes before the first letter of the second string, the B in bear. 44 00:02:38,120 --> 00:02:41,970 That is A comes before B in the alphabet. 45 00:02:41,970 --> 00:02:45,260 Letter strings will always come after number strings too. 46 00:02:45,260 --> 00:02:50,027 That is if you compared a number and a letter, a number is less than a letter. 47 00:02:50,027 --> 00:02:55,190 JavaScript provides two ways to test if values are equal. 48 00:02:55,190 --> 00:02:58,820 The double equal sign is called the equality operator. 49 00:02:58,820 --> 00:03:03,410 It lets you test if two values are the same, but it makes some allowances for 50 00:03:03,410 --> 00:03:05,570 different types of values. 51 00:03:05,570 --> 00:03:10,120 For example, three inside of quotes is a string. 52 00:03:10,120 --> 00:03:12,810 And three without quotes is a number. 53 00:03:12,810 --> 00:03:15,980 But if you use the double equal sign than the browser actually 54 00:03:15,980 --> 00:03:20,140 converts the string to a number before comparing them, like this. 55 00:03:21,300 --> 00:03:22,930 This comparison is true, 56 00:03:22,930 --> 00:03:27,090 because the JavaScript interpreter converts the string to the number three. 57 00:03:27,090 --> 00:03:31,020 And says that, yes, the number three is the same as the second number three. 58 00:03:32,290 --> 00:03:36,510 The triple equal sign is called a strict equality operator. 59 00:03:36,510 --> 00:03:39,190 It compares the type as well as the value. 60 00:03:40,400 --> 00:03:45,360 In this case, the comparison is false, because one value is a string, and 61 00:03:45,360 --> 00:03:46,690 the other is a number. 62 00:03:46,690 --> 00:03:50,520 They aren't the same type of value, so they aren't equal. 63 00:03:50,520 --> 00:03:53,850 It might seem like using the double equals operator is better, 64 00:03:53,850 --> 00:03:55,750 since it seems more flexible. 65 00:03:55,750 --> 00:03:58,500 However, professional JavaScript programmers tend to 66 00:03:58,500 --> 00:04:00,900 use the triple equals operator. 67 00:04:00,900 --> 00:04:03,150 And avoid the double equals. 68 00:04:03,150 --> 00:04:04,000 Why? Because you 69 00:04:04,000 --> 00:04:08,930 can get into some weird situations when using just the double equals operator. 70 00:04:08,930 --> 00:04:10,670 Here's one example. 71 00:04:10,670 --> 00:04:11,650 This is true. 72 00:04:11,650 --> 00:04:12,420 Huh? 73 00:04:12,420 --> 00:04:14,260 An empty string is equal to zero? 74 00:04:14,260 --> 00:04:19,320 But if you use the strict equality operator the result is false. 75 00:04:19,320 --> 00:04:20,100 What you'd expect. 76 00:04:21,400 --> 00:04:24,790 In many cases the two equality operators work the same, but 77 00:04:24,790 --> 00:04:27,690 there are a few cases like the one I just showed you where 78 00:04:27,690 --> 00:04:31,290 the doubled equal sign will get you into a lot of trouble. 79 00:04:31,290 --> 00:04:36,110 In this course I'll be using the strict or triple equals operator. 80 00:04:36,110 --> 00:04:39,970 Let's look at a few examples using the strict equality operator. 81 00:04:39,970 --> 00:04:41,540 This comparison is false. 82 00:04:41,540 --> 00:04:43,390 Because the two numbers aren't equal. 83 00:04:44,580 --> 00:04:49,280 This one is also false because those strings have different letters in them. 84 00:04:49,280 --> 00:04:54,100 Whereas this one is true because those strings are the same. 85 00:04:54,100 --> 00:04:58,650 But remember that java script is case sensitive so, this comparison is 86 00:04:58,650 --> 00:05:04,030 false because the first string has all lower case and the second doesn't. 87 00:05:04,030 --> 00:05:06,900 A lower case j isn't equal to an upper case J. 88 00:05:07,970 --> 00:05:13,090 JavaScript also provides a way to test whether two values are not equal. 89 00:05:13,090 --> 00:05:15,620 There's the basic not equal operator. 90 00:05:15,620 --> 00:05:19,200 It's an exclamation mark followed by an equals sign. 91 00:05:19,200 --> 00:05:23,120 The exclamation point is called a not operator so 92 00:05:23,120 --> 00:05:25,880 exclamation equals is not equal to. 93 00:05:26,950 --> 00:05:29,630 It's better to use the strict not equals. 94 00:05:29,630 --> 00:05:32,340 That exclamation equals equals, for 95 00:05:32,340 --> 00:05:36,490 the same reasons it's better to use the strict equality comparison operator. 96 00:05:37,500 --> 00:05:41,710 Let's look at a few examples of the strict not equals operator. 97 00:05:41,710 --> 00:05:45,040 This comparison is true because ten isn't equal to nine. 98 00:05:46,200 --> 00:05:49,790 This is also true because the value on the left is a string and 99 00:05:49,790 --> 00:05:51,880 the value on the right is a number. 100 00:05:51,880 --> 00:05:54,240 So the types don't match. 101 00:05:54,240 --> 00:05:57,460 This comparison is also true because the upper case R in 102 00:05:57,460 --> 00:06:02,260 the first string isn't the same as the lower case r in the second string. 103 00:06:02,260 --> 00:06:05,460 This comparison is false because the two numbers are the same.