1 00:00:00,700 --> 00:00:05,322 So far, the conditional statements we've created provide just two possible paths. 2 00:00:05,322 --> 00:00:08,300 Let's call them door number one, door number two. 3 00:00:08,300 --> 00:00:12,260 If a condition is true, like the user entered a name in an input field, 4 00:00:12,260 --> 00:00:14,590 the program passes through door number one. 5 00:00:14,590 --> 00:00:18,390 But if the condition is false, then the program passes through door number two. 6 00:00:19,840 --> 00:00:22,480 But what if you want many different outcomes, not just two? 7 00:00:23,990 --> 00:00:27,270 In our daily lives, we're used to many possible outcomes. 8 00:00:27,270 --> 00:00:29,720 For example, say it's your day off from work and 9 00:00:29,720 --> 00:00:32,145 you're trying to figure out what to do for the day. 10 00:00:32,145 --> 00:00:34,610 Your first step might be to find out what the weather's like. 11 00:00:34,610 --> 00:00:36,150 Based on this, you could plan your day. 12 00:00:37,270 --> 00:00:38,590 For example, if it's hot and 13 00:00:38,590 --> 00:00:42,900 sunny, you might go swimming, if it's snowing, you might go sliding or skiing. 14 00:00:42,900 --> 00:00:45,350 If it's raining, you might stay in and read a book. 15 00:00:45,350 --> 00:00:50,890 You can't do all three, only one, and each choice is based on a series of conditions. 16 00:00:50,890 --> 00:00:54,180 If it's hot and sunny, if it's snowing, if it's raining. 17 00:00:55,580 --> 00:00:57,490 JavaScript also includes a way for 18 00:00:57,490 --> 00:01:02,020 a program to provide multiple outcomes using what's called an else if clause. 19 00:01:03,440 --> 00:01:08,554 All right, now let's open the else-if.js file inside the js folder and 20 00:01:08,554 --> 00:01:14,268 as usual, update the script tag and index.html so that it points to else-if.js 21 00:01:17,716 --> 00:01:21,550 So else if works like this, you start with an if statement. 22 00:01:22,850 --> 00:01:27,820 Then add an else if clause, which provides another condition to test. 23 00:01:27,820 --> 00:01:30,080 So now you have two conditions. 24 00:01:30,080 --> 00:01:32,020 If the first one is true, 25 00:01:32,020 --> 00:01:36,430 then the code in the first block between the first pair of braces runs. 26 00:01:36,430 --> 00:01:38,480 Let's call this door number one. 27 00:01:38,480 --> 00:01:43,950 However, if the first condition is false, then we test a second condition. 28 00:01:43,950 --> 00:01:46,230 If the answer to that condition is true, 29 00:01:46,230 --> 00:01:49,330 then the code in the second code block runs. 30 00:01:49,330 --> 00:01:50,690 Let's call this door number two. 31 00:01:51,760 --> 00:01:56,340 The second condition is not tested if the first condition is true. 32 00:01:56,340 --> 00:01:57,570 This is important. 33 00:01:57,570 --> 00:02:02,500 You'll only get to the second condition if the first is false. 34 00:02:02,500 --> 00:02:07,570 Now, if neither of the conditions is true, you might want to provide a fallback, 35 00:02:07,570 --> 00:02:10,600 door number three, so that the program does something. 36 00:02:10,600 --> 00:02:13,098 You can do this by adding an else clause to the end. 37 00:02:15,828 --> 00:02:19,970 Notice how there is no condition for this last part. 38 00:02:19,970 --> 00:02:24,342 However, the code block at the end only ever runs if both of 39 00:02:24,342 --> 00:02:26,853 the conditions above are false. 40 00:02:33,028 --> 00:02:37,140 And there's no limit to the number of else if clauses you can add. 41 00:02:37,140 --> 00:02:40,860 But remember that you must start with an if statement and 42 00:02:40,860 --> 00:02:44,760 the else clause, if there is one, must go at the end. 43 00:02:44,760 --> 00:02:49,890 So now there is three test conditions and four possible outcomes. 44 00:02:49,890 --> 00:02:54,730 And keep in mind that as soon as one test condition is true, the code for 45 00:02:54,730 --> 00:02:58,170 that condition runs, and all the conditions following, 46 00:02:58,170 --> 00:03:00,280 including the very last else clause are skipped. 47 00:03:01,530 --> 00:03:05,150 Now, let's see how you might use this conditional statement in a program. 48 00:03:05,150 --> 00:03:08,080 Earlier, I gave some examples of activities you can do based on weather 49 00:03:08,080 --> 00:03:08,770 conditions. 50 00:03:08,770 --> 00:03:11,860 For instance, if it's snowing, you could go sledding. 51 00:03:11,860 --> 00:03:15,354 Your first step might be to find out what the weather is like, 52 00:03:15,354 --> 00:03:17,554 based on that you could plan your day. 53 00:03:17,554 --> 00:03:25,360 And the first condition, I'll check if weather strictly equals the string, sun. 54 00:03:25,360 --> 00:03:31,119 If it's sun, log the message it's sunny, so I'm going swimming. 55 00:03:39,874 --> 00:03:47,293 In the else if clause, I can check if weather strictly equals rain. 56 00:03:49,674 --> 00:03:55,199 And if it's rain, log it's raining, so I will read a book. 57 00:04:03,867 --> 00:04:10,775 I'll have the next else if clause check if weather strictly equals snow. 58 00:04:13,395 --> 00:04:19,239 And if so, then log the message, it's snowing, so I'm going sledding. 59 00:04:27,172 --> 00:04:31,816 Now, the else block at the end only ever runs if all three of these 60 00:04:31,816 --> 00:04:34,190 conditions are false. 61 00:04:34,190 --> 00:04:38,203 In that case, log the message, I don't know what I'm doing today. 62 00:04:47,366 --> 00:04:52,231 Before testing this code, I need to declare the weather variable, and 63 00:04:52,231 --> 00:04:55,460 assign it either sun, rain, or snow. 64 00:04:55,460 --> 00:04:56,696 I'll try rain first. 65 00:04:59,153 --> 00:05:00,724 I'll save my code. 66 00:05:00,724 --> 00:05:04,951 And over in the console, since weather = rain, I see the message, 67 00:05:04,951 --> 00:05:07,338 it's raining, so I will read a book. 68 00:05:10,109 --> 00:05:12,359 I'll try sun. 69 00:05:14,587 --> 00:05:15,622 Then snow. 70 00:05:19,900 --> 00:05:21,660 And it works. 71 00:05:21,660 --> 00:05:25,590 Now if I change the value of weather to an empty string or 72 00:05:25,590 --> 00:05:29,700 anything other than rain, sun, or snow, for example. 73 00:05:29,700 --> 00:05:32,720 The console prints, I don't know what I'm doing today. 74 00:05:34,610 --> 00:05:38,945 So as you've learned, an else if clause provides a way to make a program more 75 00:05:38,945 --> 00:05:42,830 flexible and able to respond on to multiple conditional outcomes.