1 00:00:00,090 --> 00:00:01,770 - Now this is gonna be another really quick video 2 00:00:01,770 --> 00:00:03,030 where I talk about how you can deal with 3 00:00:03,030 --> 00:00:05,250 optional parameters in a function. 4 00:00:05,250 --> 00:00:06,210 Let's say I have a function 5 00:00:06,210 --> 00:00:08,790 that's gonna be print name and age. 6 00:00:08,790 --> 00:00:10,470 It's gonna take in a name and an age. 7 00:00:10,470 --> 00:00:12,330 So this age is a number 8 00:00:12,330 --> 00:00:14,490 and the name here is going to be a string. 9 00:00:14,490 --> 00:00:16,290 And we'll just define this function really quickly 10 00:00:16,290 --> 00:00:20,910 as a simple console log of name and age. 11 00:00:20,910 --> 00:00:21,960 Here we go. 12 00:00:21,960 --> 00:00:24,540 What I can do is I can call print name and age 13 00:00:24,540 --> 00:00:27,780 and I can pass it in a name and I can pass it in an age. 14 00:00:27,780 --> 00:00:29,160 That works just fine. 15 00:00:29,160 --> 00:00:31,727 But what happens if I want this age property to be optional? 16 00:00:31,727 --> 00:00:34,350 'Cause in JavaScript, we know there's often times 17 00:00:34,350 --> 00:00:35,580 where you want to create a function 18 00:00:35,580 --> 00:00:37,290 where sometimes you pass it one parameter, 19 00:00:37,290 --> 00:00:38,550 sometimes you pass a two, 20 00:00:38,550 --> 00:00:40,440 sometimes you pass a three and so on. 21 00:00:40,440 --> 00:00:42,750 And in our case, I want this age to be optional. 22 00:00:42,750 --> 00:00:43,620 This is really common 23 00:00:43,620 --> 00:00:45,510 if you have like an options argument. 24 00:00:45,510 --> 00:00:46,860 For example, we have some options 25 00:00:46,860 --> 00:00:47,940 and they're set to an object 26 00:00:47,940 --> 00:00:49,380 with some different types inside of it. 27 00:00:49,380 --> 00:00:50,340 That's a very common thing 28 00:00:50,340 --> 00:00:52,560 that you would want to be optional inside a JavaScript, 29 00:00:52,560 --> 00:00:54,780 because by default you just use the default values, 30 00:00:54,780 --> 00:00:56,220 otherwise it's going to fall back 31 00:00:56,220 --> 00:00:58,920 to actually using whatever values you pass to it. 32 00:00:58,920 --> 00:01:00,570 What we can do is we can actually emulate that 33 00:01:00,570 --> 00:01:02,520 by instead here just having our options. 34 00:01:02,520 --> 00:01:05,460 And let's just say our options is going to be debug mode 35 00:01:05,460 --> 00:01:07,920 and this is going to be a boolean, just like that. 36 00:01:07,920 --> 00:01:09,540 So there's our option types. 37 00:01:09,540 --> 00:01:11,790 What we can do is we can pass in here an object 38 00:01:11,790 --> 00:01:13,410 that would have debug mode of true. 39 00:01:13,410 --> 00:01:14,610 We can pass this in if we want, 40 00:01:14,610 --> 00:01:16,830 but if we leave it off it's going to give us an error, 41 00:01:16,830 --> 00:01:18,720 which is probably not something that we want. 42 00:01:18,720 --> 00:01:20,100 So in our case, to make this optional 43 00:01:20,100 --> 00:01:21,867 all we have to do is just put a question mark after it. 44 00:01:21,867 --> 00:01:23,220 And that's just telling TypeScript, 45 00:01:23,220 --> 00:01:25,980 hey, this is now an optional property. 46 00:01:25,980 --> 00:01:27,480 Now what we can do inside of here 47 00:01:27,480 --> 00:01:30,330 is we can pass it along if we want, as you can see here, 48 00:01:30,330 --> 00:01:31,440 and it's going to work fine. 49 00:01:31,440 --> 00:01:32,550 Otherwise we can leave it off, 50 00:01:32,550 --> 00:01:34,593 and again, it's going to work just fine.