1
00:00:02,600 --> 00:00:04,680
Now building up on what we learned

2
00:00:04,680 --> 00:00:07,470
in the last lecture about storing Booleans

3
00:00:07,470 --> 00:00:09,240
and having checks like this,

4
00:00:09,240 --> 00:00:12,210
there is another important concept in JavaScript,

5
00:00:12,210 --> 00:00:15,780
which you don't find in most programming languages,

6
00:00:15,780 --> 00:00:18,510
but which is a key part of JavaScript,

7
00:00:18,510 --> 00:00:20,743
and which you therefore also have to know.

8
00:00:21,790 --> 00:00:24,590
Let's say we have a constant,

9
00:00:24,590 --> 00:00:27,249
or a variable, doesn't matter,

10
00:00:27,249 --> 00:00:28,749
"enteredUserName",

11
00:00:30,270 --> 00:00:31,870
and that could be Maximilian.

12
00:00:31,870 --> 00:00:33,670
It does not matter.

13
00:00:33,670 --> 00:00:36,140
Now let's assume this is again a value

14
00:00:36,140 --> 00:00:40,350
which we actually fetch from some input field,

15
00:00:40,350 --> 00:00:42,370
just like we do it in demo.js.

16
00:00:42,370 --> 00:00:44,940
There, we also get some entered value.

17
00:00:44,940 --> 00:00:47,300
Let's assume the same is the case here

18
00:00:47,300 --> 00:00:48,637
for "enteredUserName."

19
00:00:50,100 --> 00:00:54,220
Now we might have some code that should only execute

20
00:00:54,220 --> 00:00:57,450
if this is a non empty string.

21
00:00:57,450 --> 00:01:01,107
And then, we could say, "Input is valid."

22
00:01:02,330 --> 00:01:05,010
So, we could check if this is a non empty string,

23
00:01:05,010 --> 00:01:07,683
if it has a more than zero characters.

24
00:01:08,760 --> 00:01:12,410
Now for that, we can write "enteredUserName"

25
00:01:12,410 --> 00:01:16,670
and get the length of that string with the length property

26
00:01:16,670 --> 00:01:18,923
and check if that's greater than zero.

27
00:01:19,890 --> 00:01:22,380
And if it is, which it in this case is,

28
00:01:22,380 --> 00:01:25,987
then I can save this and I get, "Input is valid."

29
00:01:27,610 --> 00:01:29,960
But, if that's all we care about,

30
00:01:29,960 --> 00:01:33,060
there is a even shorter way of checking this.

31
00:01:33,060 --> 00:01:37,480
We can also write "if enteredUserName," like that.

32
00:01:37,480 --> 00:01:41,520
And if I save that, I also get, "Input is valid."

33
00:01:41,520 --> 00:01:46,520
And if I go back and I turn this into an empty string,

34
00:01:46,580 --> 00:01:49,480
so no input was provided,

35
00:01:49,480 --> 00:01:52,510
then I actually don't get this output.

36
00:01:52,510 --> 00:01:56,380
So this also works as we would want it to work.

37
00:01:56,380 --> 00:01:57,433
But why?

38
00:01:58,370 --> 00:02:01,380
Because JavaScript has this concept of

39
00:02:01,380 --> 00:02:04,573
truthy and falsy values.

40
00:02:05,540 --> 00:02:08,919
Now, we have true and false for Booleans,

41
00:02:08,919 --> 00:02:12,420
but in a place where JavaScript wants a Boolean,

42
00:02:12,420 --> 00:02:14,800
like in this if condition here,

43
00:02:14,800 --> 00:02:18,350
if you provide a non-Boolean value,

44
00:02:18,350 --> 00:02:21,990
JavaScript will basically try to convert

45
00:02:21,990 --> 00:02:24,670
that provided value to a Boolean,

46
00:02:24,670 --> 00:02:28,380
and it has certain rules for that conversion.

47
00:02:28,380 --> 00:02:31,420
Now I got dedicated deep dive courses

48
00:02:31,420 --> 00:02:35,080
where I dive way deeper into all the niche details

49
00:02:35,080 --> 00:02:37,600
of how JavaScript does this conversion

50
00:02:37,600 --> 00:02:39,960
and I dive into all the tricky cases

51
00:02:39,960 --> 00:02:42,050
where this conversion might be strange,

52
00:02:42,050 --> 00:02:44,450
but it's generally straightforward.

53
00:02:44,450 --> 00:02:48,250
An empty string, or the number zero,

54
00:02:48,250 --> 00:02:49,993
will be treated as false.

55
00:02:50,920 --> 00:02:52,930
A string with zero inside of it

56
00:02:52,930 --> 00:02:54,970
would not be treated as false

57
00:02:54,970 --> 00:02:57,890
since this is not an empty string.

58
00:02:57,890 --> 00:03:02,890
So empty string or zero are basically treated like false.

59
00:03:03,810 --> 00:03:05,900
A string with anything in there,

60
00:03:05,900 --> 00:03:07,750
even if it's the text "false,"

61
00:03:07,750 --> 00:03:10,970
would be treated as true because it's not empty.

62
00:03:10,970 --> 00:03:12,520
So it's really only the question of

63
00:03:12,520 --> 00:03:14,160
whether it's empty or not that matters.

64
00:03:14,160 --> 00:03:17,750
Even a blank character would count as not empty

65
00:03:17,750 --> 00:03:19,403
and therefore as true.

66
00:03:20,570 --> 00:03:23,870
And hence, if you use a string or a number here,

67
00:03:23,870 --> 00:03:26,170
then this will be treated as true,

68
00:03:26,170 --> 00:03:29,740
if it's not empty or not zero,

69
00:03:29,740 --> 00:03:31,300
and it will be treated as false

70
00:03:31,300 --> 00:03:33,843
if it is an empty string or a zero number.

71
00:03:34,700 --> 00:03:37,700
And that's why this check also works.

72
00:03:37,700 --> 00:03:39,670
This might sound like a niche case,

73
00:03:39,670 --> 00:03:41,650
but it actually isn't.

74
00:03:41,650 --> 00:03:44,650
Very often you will perform checks like this

75
00:03:44,650 --> 00:03:46,700
simply because it's shorter

76
00:03:46,700 --> 00:03:49,160
than checking for length greater than zero,

77
00:03:49,160 --> 00:03:52,090
and it has exactly the same result.

78
00:03:52,090 --> 00:03:54,050
So whilst you could do that,

79
00:03:54,050 --> 00:03:55,880
this is simply shorter

80
00:03:55,880 --> 00:04:00,460
and it works because of this truthy falsy behavior

81
00:04:00,460 --> 00:04:02,433
that's built into JavaScript.

82
00:04:03,470 --> 00:04:05,520
Now, if that's still a bit much

83
00:04:05,520 --> 00:04:07,550
and a bit confusing at this point,

84
00:04:07,550 --> 00:04:08,670
again, don't worry.

85
00:04:08,670 --> 00:04:11,320
We'll have many examples throughout this course,

86
00:04:11,320 --> 00:04:13,810
but as we are talking about if statements

87
00:04:13,810 --> 00:04:15,220
and Booleans right now,

88
00:04:15,220 --> 00:04:17,290
that's definitely all of the concept

89
00:04:17,290 --> 00:04:20,063
that needed to be introduced at this point.

