1
00:00:02,120 --> 00:00:07,790
Often when you write code, you can of course be sure about the existence of certain properties in your

2
00:00:08,320 --> 00:00:08,810
objects,

3
00:00:08,810 --> 00:00:15,200
for example here, I know I will have an info property in every movie because I myself wrote the code

4
00:00:15,260 --> 00:00:16,890
that adds it.

5
00:00:17,040 --> 00:00:23,180
Now depending on how dynamic your program is and how much of the data that you're working with has been

6
00:00:23,180 --> 00:00:28,190
entered by the end users where you can't predict at the point of time you're writing your code

7
00:00:28,310 --> 00:00:34,730
what will be inside your objects, you might want to validate whether something is present

8
00:00:34,730 --> 00:00:39,830
or maybe you have some part in your code where this info property would be deleted and you're not

9
00:00:39,830 --> 00:00:42,390
sure whether that ran already or not.

10
00:00:42,410 --> 00:00:48,320
So sometimes you want to check if a property is part of an object and you can do this in Javascript

11
00:00:48,350 --> 00:00:49,480
as well,

12
00:00:49,520 --> 00:00:55,490
for example here in an if check, you can use a special keyword that helps you find out if something is part

13
00:00:55,760 --> 00:00:59,750
of an object, you can use the in keyword.

14
00:00:59,990 --> 00:01:06,720
It works such that you check if, then the property name you want to check as a string, info, then the in

15
00:01:06,740 --> 00:01:11,460
keyword and then your object. So we can check if info is in movie,

16
00:01:11,510 --> 00:01:16,730
if this yields true, we know movie has an info property and now we could write code that should only run

17
00:01:16,760 --> 00:01:18,500
if we have that info property.

18
00:01:18,560 --> 00:01:24,020
if we want to check the opposite, we can wrap this into an extra pair of parentheses and add the exclamation

19
00:01:24,020 --> 00:01:24,320
mark

20
00:01:24,320 --> 00:01:31,980
operator here to check if that returns a false value basically. So that's the in operator,

21
00:01:31,980 --> 00:01:38,220
of course here we could have also checked if info has a truthy value after setting it here because

22
00:01:38,220 --> 00:01:44,420
that will hold undefined if there is no info key, so that would have been a perfectly viable alternative

23
00:01:44,700 --> 00:01:49,470
but if you have some other code where you don't plan on using object destructuring and still it matters

24
00:01:49,470 --> 00:01:55,410
to you to know whether a property is part or not, you can use the in operator. Another way of checking

25
00:01:55,410 --> 00:02:01,020
this is to just use movie.info and compare if that is equal to undefined.

26
00:02:01,110 --> 00:02:07,110
You know that a property has a value of undefined if it's not there, if it was deleted or if you manually

27
00:02:07,110 --> 00:02:11,210
set it to undefined or if it never was defined in the first place.

28
00:02:11,280 --> 00:02:16,950
So if this is equal to undefined, then you know this property does not exist,

29
00:02:16,950 --> 00:02:20,130
so if this is equal to undefined, we have no info property,

30
00:02:20,160 --> 00:02:24,200
that's another way of determining whether you have that property or not.

31
00:02:24,210 --> 00:02:29,910
Now here, I don't need that check but this is how you could check for the existence of a property in

32
00:02:29,910 --> 00:02:35,220
case you need it in your code to avoid errors where you try to work with a property that's not there

33
00:02:35,430 --> 00:02:40,620
or where you maybe want to assign some dummy default value to a property if it doesn't exist yet.
