1 00:00:00,180 --> 00:00:01,440 - Now in this video, we're gonna be talking 2 00:00:01,440 --> 00:00:03,559 about a special type called the void type. 3 00:00:03,559 --> 00:00:04,770 And it's gonna be a rather short video. 4 00:00:04,770 --> 00:00:06,180 For example, if we hover over the sum, 5 00:00:06,180 --> 00:00:08,010 you know that the return type of this sum function 6 00:00:08,010 --> 00:00:10,620 is a number and that's because TypeScript infers that. 7 00:00:10,620 --> 00:00:12,420 Or we can manually come in here and say 8 00:00:12,420 --> 00:00:15,030 that the return type is going to be number if we want. 9 00:00:15,030 --> 00:00:16,140 But what happens if a function 10 00:00:16,140 --> 00:00:18,060 doesn't actually return anything? 11 00:00:18,060 --> 00:00:19,410 Let's create a brand new function. 12 00:00:19,410 --> 00:00:20,790 This one's just gonna be print name. 13 00:00:20,790 --> 00:00:23,400 We're gonna pass it along a name which is a string 14 00:00:23,400 --> 00:00:26,490 and it's just gonna console.log that name variable. 15 00:00:26,490 --> 00:00:28,740 This function has no return at all. 16 00:00:28,740 --> 00:00:30,720 So what is the return type? 17 00:00:30,720 --> 00:00:31,980 Well, if we hover over print name, 18 00:00:31,980 --> 00:00:34,410 you can see the return type is set to void. 19 00:00:34,410 --> 00:00:35,700 We could also manually say 20 00:00:35,700 --> 00:00:38,250 that this is going to be a void return, as well. 21 00:00:38,250 --> 00:00:40,020 And all that means is that your function 22 00:00:40,020 --> 00:00:43,620 has no return keyword and it doesn't return anything. 23 00:00:43,620 --> 00:00:45,390 This is a slightly different keyword 24 00:00:45,390 --> 00:00:48,390 than undefined, because in JavaScript, by default, 25 00:00:48,390 --> 00:00:51,720 if you run a function and it doesn't return anything at all 26 00:00:51,720 --> 00:00:54,720 it'll output undefined if you try to set a variable to it. 27 00:00:54,720 --> 00:00:58,350 So if I just say const a equals print name, 28 00:00:58,350 --> 00:01:00,120 pass it in a name, 29 00:01:00,120 --> 00:01:02,880 this a variable is technically undefined 30 00:01:02,880 --> 00:01:04,590 by JavaScript standards. 31 00:01:04,590 --> 00:01:06,780 But in TypeScript, we actually differentiate 32 00:01:06,780 --> 00:01:09,270 undefined from void functions 33 00:01:09,270 --> 00:01:11,790 'cause they have a slightly different meaning to them. 34 00:01:11,790 --> 00:01:14,100 When I say that a function returns nothing, 35 00:01:14,100 --> 00:01:16,800 and by that I mean the return type is set to void, 36 00:01:16,800 --> 00:01:19,200 I'm essentially saying that there is nothing you can do 37 00:01:19,200 --> 00:01:20,430 with the return from this function. 38 00:01:20,430 --> 00:01:22,620 It doesn't return anything and nothing that comes 39 00:01:22,620 --> 00:01:24,960 from it is useful in any way at all. 40 00:01:24,960 --> 00:01:26,250 While if I explicitly say 41 00:01:26,250 --> 00:01:28,320 that the function returns undefined, 42 00:01:28,320 --> 00:01:30,180 I can at least use that in my TypeScript code 43 00:01:30,180 --> 00:01:32,610 because I know that this thing is now undefined 44 00:01:32,610 --> 00:01:33,750 that it is returning from it. 45 00:01:33,750 --> 00:01:36,060 It's like an explicit return type of undefined, 46 00:01:36,060 --> 00:01:37,800 while void is just explicitly saying 47 00:01:37,800 --> 00:01:39,120 this returns nothing at all 48 00:01:39,120 --> 00:01:41,310 and it never will return anything at all. 49 00:01:41,310 --> 00:01:43,470 So there's a slight difference between those two. 50 00:01:43,470 --> 00:01:44,700 For example, if I were to just come in here 51 00:01:44,700 --> 00:01:46,740 and I place a return statement, now when I look 52 00:01:46,740 --> 00:01:48,960 at my print name, you can see it's still a void type. 53 00:01:48,960 --> 00:01:51,630 'cause again, I'm returning nothing all from my function. 54 00:01:51,630 --> 00:01:54,360 If I instead explicitly return undefined, 55 00:01:54,360 --> 00:01:55,800 you can now see the return type 56 00:01:55,800 --> 00:01:59,340 of this function has changed to undefined from void. 57 00:01:59,340 --> 00:02:00,750 So if you want to return undefined 58 00:02:00,750 --> 00:02:03,480 from a function and use it as undefined in TypeScript, 59 00:02:03,480 --> 00:02:05,700 you need to manually return undefined. 60 00:02:05,700 --> 00:02:08,820 But in 99% of cases, void is going to work just fine 61 00:02:08,820 --> 00:02:09,780 'cause if you have a function 62 00:02:09,780 --> 00:02:12,270 that you don't want to return anything from, or you are 63 00:02:12,270 --> 00:02:15,150 returning early by just having the return statement, 64 00:02:15,150 --> 00:02:17,100 it makes sense that this will be a void type 65 00:02:17,100 --> 00:02:19,823 'cause essentially it's saying there's nothing there at all.