1 00:00:00,180 --> 00:00:00,690 Welcome back. 2 00:00:01,140 --> 00:00:02,370 Let's talk about. 3 00:00:03,490 --> 00:00:07,860 Knowledge coalescing operator, and it's this double questionmark. 4 00:00:08,050 --> 00:00:12,730 So remember, we learned about the big event and we also learned about the optional training with the 5 00:00:12,730 --> 00:00:15,380 questionmark and the period just like this. 6 00:00:15,820 --> 00:00:23,200 So what's this double questionmark and this double questionmark can often be used instead of what we've 7 00:00:23,200 --> 00:00:25,450 seen before, the or operator. 8 00:00:26,110 --> 00:00:27,370 So let me show you what I mean. 9 00:00:28,380 --> 00:00:35,220 Let's imagine that we have a new property here and we'll call it power. 10 00:00:36,230 --> 00:00:39,860 And power for Pikachu is. 11 00:00:43,770 --> 00:00:55,290 Now, if I wanted to get the power of my Pokémon, then I can simply do this and grab the power or the 12 00:00:55,290 --> 00:00:56,970 property power lining. 13 00:00:58,590 --> 00:01:08,280 But let's say we weren't sure that we had this property for the Pokémon, so we can't really do questionmark 14 00:01:08,280 --> 00:01:11,250 period because, well, that doesn't really make sense. 15 00:01:11,280 --> 00:01:17,550 Remember, the optional chaining operator works on objects and checking their properties, but power 16 00:01:17,550 --> 00:01:19,330 is the end of the chain. 17 00:01:19,380 --> 00:01:20,540 We can't really do anything. 18 00:01:20,700 --> 00:01:24,600 So most of the time what we would do is do an hour operator. 19 00:01:24,930 --> 00:01:30,630 So if let's say Pikachu doesn't have a power, we can just say no power. 20 00:01:32,080 --> 00:01:41,980 So let's imagine here that we have an empty string or maybe not even the property, and if I copy this 21 00:01:41,980 --> 00:01:43,750 over and run this. 22 00:01:45,570 --> 00:01:49,540 I get no power that makes sense, right? 23 00:01:50,400 --> 00:01:53,240 But what if we had power in here? 24 00:01:53,850 --> 00:01:55,850 But it's an empty string. 25 00:01:56,580 --> 00:01:57,570 What would happen then? 26 00:01:58,810 --> 00:02:01,270 Well, if I run this again. 27 00:02:06,880 --> 00:02:08,919 I get no power again. 28 00:02:10,139 --> 00:02:19,620 And that is because this or operator works by checking if this is Truthy, if it's not Truthy, then 29 00:02:19,620 --> 00:02:21,540 it resorts to no power. 30 00:02:22,040 --> 00:02:23,310 That's how JavaScript works. 31 00:02:23,500 --> 00:02:28,810 So this empty string evaluates as false and I get no power. 32 00:02:29,310 --> 00:02:32,190 What if the power was false? 33 00:02:33,340 --> 00:02:42,370 Once again, this will resort to force and you'll get no power, but what if we wanted this to be an 34 00:02:42,370 --> 00:02:45,070 empty string instead of no power? 35 00:02:45,160 --> 00:02:49,150 We want to check if this is no or undefined. 36 00:02:49,810 --> 00:02:51,940 Only then do we want to give no power. 37 00:02:52,210 --> 00:02:57,430 Otherwise, maybe this Pikachu just is still evolving and doesn't have any power. 38 00:02:57,430 --> 00:02:59,660 But we don't necessarily want to say no power. 39 00:03:00,610 --> 00:03:04,780 Well, in that case, we can do the knowledge coalescing. 40 00:03:05,410 --> 00:03:11,560 And this instead of the OR operator doesn't check if a value is falsey. 41 00:03:11,890 --> 00:03:15,660 Instead, it checks if it's null or undefined. 42 00:03:16,480 --> 00:03:19,210 So things like zero, for example. 43 00:03:19,570 --> 00:03:21,550 If we do that, well, let's find out. 44 00:03:23,750 --> 00:03:28,460 If I copy this, I get power of zero. 45 00:03:28,910 --> 00:03:30,470 If this was an or. 46 00:03:33,020 --> 00:03:36,530 In that case, the result would be different. 47 00:03:38,020 --> 00:03:43,240 The result would be no power, even though we actually have a value. 48 00:03:43,870 --> 00:03:50,080 So, again, knowledge coalescing is really, really useful for those occasions that you might use the 49 00:03:50,080 --> 00:03:51,140 or operator. 50 00:03:51,880 --> 00:03:52,990 Pretty, pretty cool. 51 00:03:53,560 --> 00:03:53,980 All right. 52 00:03:54,280 --> 00:03:55,350 I think that's enough. 53 00:03:55,360 --> 00:03:57,730 There's a few more to cover with 20. 54 00:03:58,150 --> 00:04:04,150 But again, we're going to encounter those throughout the rest of the sections because they require 55 00:04:04,150 --> 00:04:06,940 a bit of other knowledge that we're going to accumulate. 56 00:04:07,180 --> 00:04:10,090 So let's take a break and I'll see you in the next one.