1 00:00:09,180 --> 00:00:15,210 All right, everyone, let's continue talking about conditionals, so now I want to start talking about 2 00:00:15,210 --> 00:00:23,370 evaluating values as conditions in JavaScript, just like Python PDB and many other programming languages, 3 00:00:23,520 --> 00:00:28,790 we can evaluate values as true or false without booleans. 4 00:00:28,800 --> 00:00:31,470 So without doing actual comparisons. 5 00:00:31,680 --> 00:00:36,480 So JavaScript considers these values to be false. 6 00:00:36,780 --> 00:00:39,870 So false itself, then undefined. 7 00:00:40,050 --> 00:00:41,880 No, zero. 8 00:00:42,100 --> 00:00:47,490 And then I'm going to talk about this in a moment and an empty string. 9 00:00:48,070 --> 00:00:56,910 So if we have these values inside a test in the if statement, JavaScript is going to consider all this 10 00:00:56,910 --> 00:00:57,810 to be false. 11 00:00:58,020 --> 00:01:01,350 And as you will see, this could be very useful. 12 00:01:02,430 --> 00:01:09,060 So first, let me just talk what is the man so every time we have an error in an operation, so let's 13 00:01:09,060 --> 00:01:15,870 say we have a string with a name and we try to do something that JavaScript doesn't understand. 14 00:01:15,880 --> 00:01:21,180 So we just try to do John, divided by tree, which is something completely crazy. 15 00:01:21,210 --> 00:01:26,530 So JavaScript, it is going to return men, which means not a number. 16 00:01:26,540 --> 00:01:28,770 No, this is actually an error. 17 00:01:29,010 --> 00:01:35,850 So if there's an error, if we try to do an operation and we have this in return and we are using it 18 00:01:35,850 --> 00:01:43,170 inside the if statement, JavaScript will consider this to be false and the same thing will happen for 19 00:01:43,170 --> 00:01:44,850 these other values. 20 00:01:45,180 --> 00:01:51,240 So now let's go to Visual Studio Code and let's see a very common example about this. 21 00:01:51,450 --> 00:01:54,150 So let's say we are getting the input field. 22 00:01:54,270 --> 00:02:01,800 So a person just typed his name in an input field and we have a variable called name. 23 00:02:02,520 --> 00:02:08,020 And now let's say the person just left it blank so we don't have the name. 24 00:02:08,790 --> 00:02:12,050 It is common to use a conditional like this. 25 00:02:12,210 --> 00:02:14,040 So if name. 26 00:02:18,810 --> 00:02:26,430 Then we can print something like the name is and then print the name, but if the name is left blank, 27 00:02:27,150 --> 00:02:31,290 then we can print the name has not been informed. 28 00:02:31,440 --> 00:02:33,530 So in this case, we have it blank. 29 00:02:33,810 --> 00:02:36,350 Let's save this and let's see what happens. 30 00:02:38,340 --> 00:02:43,150 So saving this and now we can see the name has not been informed. 31 00:02:43,590 --> 00:02:45,930 Now let's try to change the variable. 32 00:02:45,940 --> 00:02:47,580 So, Ivan. 33 00:02:50,350 --> 00:02:53,200 Refresh the page again, and now we can see. 34 00:02:54,270 --> 00:02:55,540 That we have the name. 35 00:02:55,950 --> 00:02:57,820 So what just happened here? 36 00:02:58,260 --> 00:03:00,810 We are just evaluating this value. 37 00:03:00,930 --> 00:03:06,150 So instead of having to make the actual tests like if name. 38 00:03:07,050 --> 00:03:08,860 Is an empty string. 39 00:03:08,880 --> 00:03:14,840 We don't have to write all this because JavaScript can also evaluate values. 40 00:03:15,120 --> 00:03:22,680 So in this case, JavaScript just found an empty string in JavaScript, considers an empty string to 41 00:03:22,680 --> 00:03:26,330 be false, the same thing for zero. 42 00:03:26,670 --> 00:03:29,070 So if we have a total. 43 00:03:34,410 --> 00:03:35,280 Pop zero. 44 00:03:37,230 --> 00:03:39,720 We can also do, if total. 45 00:03:43,380 --> 00:03:47,130 Let's say I sold something. 46 00:03:52,370 --> 00:03:54,170 Otherwise, I sold. 47 00:03:56,600 --> 00:04:07,460 Nothing, so if I have a total of 100, then just by typing this refreshing the page, then we can see 48 00:04:07,460 --> 00:04:08,980 I sold something. 49 00:04:09,500 --> 00:04:11,960 But if the total is zero. 50 00:04:13,010 --> 00:04:17,090 Refreshing the page now we can see I sold nothing. 51 00:04:18,570 --> 00:04:25,800 So now we can see that these values are considered to be false, so from now on you will see that sometimes 52 00:04:25,800 --> 00:04:28,320 we will just ride conditionals like this. 53 00:04:28,500 --> 00:04:31,710 We don't have to actually write this test. 54 00:04:31,710 --> 00:04:37,050 If name equals an empty string, we can just test the value itself. 55 00:04:37,230 --> 00:04:44,280 So to finish with this lesson, let's just check another way of writing conditionals, which is the 56 00:04:44,280 --> 00:04:45,240 ternary. 57 00:04:45,250 --> 00:04:47,760 If so, this is the syntax. 58 00:04:47,760 --> 00:04:55,680 First we type the condition, then questionmark, then the code that should be executed. 59 00:04:55,680 --> 00:05:03,120 If this condition is true, then Callon and then the code to be executed if false. 60 00:05:03,360 --> 00:05:07,810 So now let's get the same example we just used with the name. 61 00:05:08,070 --> 00:05:09,300 So going back here. 62 00:05:10,880 --> 00:05:15,050 Let me just press control to go back. 63 00:05:22,980 --> 00:05:31,080 All right, so I'm just going to comment all this and I'm going to get the console log. 64 00:05:32,620 --> 00:05:38,860 And inside here, instead of just writing this message, I'm just going to write the ternary if so, 65 00:05:38,860 --> 00:05:39,820 how does it work? 66 00:05:40,060 --> 00:05:43,390 First, I need to type the condition. 67 00:05:43,480 --> 00:05:48,640 So the condition in this case, its name, if there's a name, then we can do something. 68 00:05:48,910 --> 00:05:54,130 Then we can use the question mark and after the question mark. 69 00:05:56,850 --> 00:06:04,860 Comes the code to be executed, if this is true in this case, we are just returning this message then 70 00:06:04,860 --> 00:06:08,430 Colin and then the code to be executed. 71 00:06:09,690 --> 00:06:11,760 If the condition is false. 72 00:06:13,150 --> 00:06:17,800 So now let's paste the message to print in case of false. 73 00:06:18,100 --> 00:06:20,170 So now we don't have a name. 74 00:06:20,770 --> 00:06:23,230 Let's see what goes to the console. 75 00:06:26,520 --> 00:06:28,800 The name has not been informed. 76 00:06:29,010 --> 00:06:32,910 Now let's try to put a name here, Mary. 77 00:06:35,100 --> 00:06:41,730 Refresh the page, and now we can see that this is working the same way, so this is good because we 78 00:06:41,730 --> 00:06:45,340 can write less code to get to the same result. 79 00:06:46,020 --> 00:06:53,340 So here we had a few lines of code and now pretty much if we don't consider this to be part of the code, 80 00:06:53,520 --> 00:06:57,900 we just have a one line to test these conditions, which is good. 81 00:06:58,410 --> 00:07:06,710 Just one more thing so we can finish this lesson we can also use or else if in this ternary conditionals. 82 00:07:07,260 --> 00:07:12,180 So let's do the same example with the above with the ages. 83 00:07:12,190 --> 00:07:16,670 So here we have the age example where we have three conditions. 84 00:07:17,280 --> 00:07:18,390 So here it is. 85 00:07:19,640 --> 00:07:21,980 Let me just copy all this. 86 00:07:28,190 --> 00:07:33,170 So we have the variable and now we can just do console, not log. 87 00:07:37,330 --> 00:07:45,070 And now this is going to happen in a very similar way, but we just need to pay attention first, we 88 00:07:45,070 --> 00:07:46,420 want the condition. 89 00:07:46,750 --> 00:07:52,680 So here we have the condition we used in the first if if this is true. 90 00:07:52,690 --> 00:07:54,280 So now questionmark. 91 00:07:56,330 --> 00:07:58,430 This is what we want to return. 92 00:07:59,590 --> 00:08:04,980 Otherwise, so now this is not an alse this is going to be another condition. 93 00:08:05,470 --> 00:08:12,340 So if you want to do this in the place where you should base the code for the house, you can just start 94 00:08:12,340 --> 00:08:16,270 another condition so you can do this as many times as you want. 95 00:08:16,590 --> 00:08:22,390 So now, instead of putting the code for the house, I'm just going to start another condition, which 96 00:08:22,390 --> 00:08:23,170 is this one. 97 00:08:25,760 --> 00:08:32,270 And now we just repeat the same thing until we are done, so in this case, this is a new condition. 98 00:08:32,280 --> 00:08:34,460 So questionmark again. 99 00:08:36,360 --> 00:08:41,010 Now, this is the message that we want to print in case this is true. 100 00:08:42,360 --> 00:08:49,230 Again, Colin, this time we don't want another condition, so now we can just go to the last one and 101 00:08:49,230 --> 00:08:50,460 print ATAT. 102 00:08:54,230 --> 00:08:56,970 So this time the person is 18. 103 00:08:56,990 --> 00:08:58,310 Let's try 80. 104 00:09:01,520 --> 00:09:06,920 Refresh the page and now we can see Senior, let's just do one more test. 105 00:09:07,760 --> 00:09:08,930 Let's try eight. 106 00:09:11,340 --> 00:09:14,820 Underage and twenty eight. 107 00:09:16,410 --> 00:09:23,880 We can see that this is working the same way, however, this time the fewer lines of code we achieved 108 00:09:24,000 --> 00:09:25,570 came with a trade off. 109 00:09:25,800 --> 00:09:33,690 Now we are making the code readability worse, because just by looking at this, it could be a bit confusing, 110 00:09:33,960 --> 00:09:35,340 not in this example. 111 00:09:35,350 --> 00:09:40,830 So when we have just one, if anyone else, then it's fine to use the ternary if. 112 00:09:41,130 --> 00:09:46,800 But if you have multiple conditions, I personally don't like to write it like this. 113 00:09:47,010 --> 00:09:48,630 I think this is confusing. 114 00:09:48,840 --> 00:09:50,820 This is a bit harder to understand. 115 00:09:50,820 --> 00:09:53,060 What is the logic inside here? 116 00:09:53,610 --> 00:09:55,440 So that was all for this lesson. 117 00:09:55,620 --> 00:10:00,220 In the next video, we're going to talk about nesting loops and conditions. 118 00:10:00,360 --> 00:10:01,160 I'll see you then.