1
00:00:02,070 --> 00:00:04,810
Now we understand how if statements work

2
00:00:04,810 --> 00:00:07,510
and how we can use comparison operators

3
00:00:07,510 --> 00:00:10,520
to check different conditions.

4
00:00:10,520 --> 00:00:13,640
There is a little extension, some extra knowledge

5
00:00:13,640 --> 00:00:16,340
you should have about Boolean values

6
00:00:16,340 --> 00:00:20,090
and if statements though, which I want to dive in now.

7
00:00:20,090 --> 00:00:22,790
And for this, I switched back to including

8
00:00:22,790 --> 00:00:27,790
the conditional code JS file here in the index HTML file.

9
00:00:28,070 --> 00:00:31,280
And in this conditional code JS file,

10
00:00:31,280 --> 00:00:33,940
I now want to show you a different way of using

11
00:00:33,940 --> 00:00:37,360
Boolean values and of writing if statements,

12
00:00:37,360 --> 00:00:41,140
which is also very common in Javascript.

13
00:00:41,140 --> 00:00:44,630
Up to this point, we always derived Boolean values

14
00:00:44,630 --> 00:00:47,370
by writing comparison code like this.

15
00:00:47,370 --> 00:00:49,830
And that is super common.

16
00:00:49,830 --> 00:00:53,930
But sometimes you also have so-called flag variables,

17
00:00:53,930 --> 00:00:58,480
which have a true or false value stored inside of them.

18
00:00:58,480 --> 00:01:01,700
And an example could be that you're building a website

19
00:01:01,700 --> 00:01:06,010
where users can log in and hence in your Javascript code

20
00:01:06,010 --> 00:01:09,160
that adds a certain functionality to the page,

21
00:01:09,160 --> 00:01:13,670
you might be storing the current log in status of the user

22
00:01:13,670 --> 00:01:16,510
because your code execution depends on whether

23
00:01:16,510 --> 00:01:19,620
a user isLoggedIn or not.

24
00:01:19,620 --> 00:01:23,020
For that, you could have a constant here or a variable,

25
00:01:23,020 --> 00:01:26,830
doesn't matter, which you could name isLoggedIn,

26
00:01:28,580 --> 00:01:31,990
which is a kind of name we haven't used up to this point,

27
00:01:31,990 --> 00:01:35,460
but which is a naming convention you'll use very often

28
00:01:35,460 --> 00:01:38,480
when you store Boolean values,

29
00:01:38,480 --> 00:01:41,690
because you want to describe what's stored in a variable.

30
00:01:41,690 --> 00:01:45,090
And here I want to store true, let's say.

31
00:01:45,090 --> 00:01:48,590
So not the result of some comparison, not a number,

32
00:01:48,590 --> 00:01:52,570
not a string, but true as a value itself.

33
00:01:52,570 --> 00:01:55,860
And then isLoggedIn is a quite fitting name

34
00:01:55,860 --> 00:01:59,920
if this variable stores the answer to the question

35
00:01:59,920 --> 00:02:03,793
whether the current website visitor is logged in or not.

36
00:02:05,180 --> 00:02:08,460
So here I'm storing true. Could be false, doesn't matter.

37
00:02:08,460 --> 00:02:12,283
It's just an example in that variable named isLoggedIn.

38
00:02:13,570 --> 00:02:16,830
And now we might have some code that should only be executed

39
00:02:16,830 --> 00:02:18,310
if this is true.

40
00:02:18,310 --> 00:02:20,350
And here, of course, it's hard coded,

41
00:02:20,350 --> 00:02:23,710
but again, we could be fetching this from some other part

42
00:02:23,710 --> 00:02:26,850
of this website and hence this could be dynamic.

43
00:02:26,850 --> 00:02:29,000
Let's imagine it is.

44
00:02:29,000 --> 00:02:33,190
Now we can write a if statement and we can, of course,

45
00:02:33,190 --> 00:02:38,190
then for example, console log user is logged in.

46
00:02:39,860 --> 00:02:40,813
We could do that.

47
00:02:41,850 --> 00:02:44,230
Now in this condition, we now might want to check

48
00:02:44,230 --> 00:02:47,130
if isLoggedIn is true or false.

49
00:02:47,130 --> 00:02:50,110
And for that, we can of course compare isLoggedIn

50
00:02:50,110 --> 00:02:54,630
to true and say this code should execute if isLoggedIn,

51
00:02:54,630 --> 00:02:57,973
this variable, has stored true as a value.

52
00:02:58,880 --> 00:03:01,270
That would work and there's nothing wrong with it.

53
00:03:01,270 --> 00:03:06,260
But keep in mind this condition here, in a if statement

54
00:03:06,260 --> 00:03:09,050
or also in an elseif block, of course,

55
00:03:09,050 --> 00:03:12,270
wants a Boolean value in the end.

56
00:03:12,270 --> 00:03:14,540
And we use comparisons like this,

57
00:03:14,540 --> 00:03:18,870
or like this because those comparisons do yield

58
00:03:18,870 --> 00:03:20,263
Boolean values.

59
00:03:21,110 --> 00:03:24,370
Now isLoggedIn actually already has

60
00:03:24,370 --> 00:03:26,830
a Boolean value stored.

61
00:03:26,830 --> 00:03:29,940
So this is kind of a redundant comparison.

62
00:03:29,940 --> 00:03:34,680
We can also just write if isLoggedIn like this.

63
00:03:34,680 --> 00:03:37,350
And if that's true, which in this case is,

64
00:03:37,350 --> 00:03:38,960
this code will execute.

65
00:03:38,960 --> 00:03:42,393
If this would be false, then this code would not execute.

66
00:03:43,290 --> 00:03:47,033
So if I save that, I get user is logged in here.

67
00:03:48,250 --> 00:03:50,890
Now let's say we would want to have the opposite check.

68
00:03:50,890 --> 00:03:53,670
And we want to say user is not logged in here.

69
00:03:53,670 --> 00:03:57,720
Then we might want to check whether isLoggedIn is false.

70
00:03:57,720 --> 00:04:01,280
And again, we could therefore write this

71
00:04:01,280 --> 00:04:05,510
and that would be correct, but we can also shorten this

72
00:04:05,510 --> 00:04:08,940
and just write if not isLoggedIn

73
00:04:08,940 --> 00:04:11,130
with that negation operator,

74
00:04:11,130 --> 00:04:12,393
this exclamation mark.

75
00:04:13,850 --> 00:04:17,170
With that we're saying, if this is not true,

76
00:04:17,170 --> 00:04:18,942
then this code should execute.

77
00:04:19,820 --> 00:04:21,060
Here, of course, it is true.

78
00:04:21,060 --> 00:04:24,680
And therefore if I save that, we have no output from that

79
00:04:24,680 --> 00:04:27,210
if statement here because we don't make it

80
00:04:27,210 --> 00:04:31,630
into this if statement with this condition and this value.

81
00:04:31,630 --> 00:04:35,100
But knowing that you can store Booleans in variables

82
00:04:35,100 --> 00:04:37,800
as I'm doing it here, and how you should then name

83
00:04:37,800 --> 00:04:41,060
these variables, that you should name them

84
00:04:41,060 --> 00:04:44,750
as if they answer a question, and that this is also

85
00:04:44,750 --> 00:04:47,270
a common thing to do and how you can then

86
00:04:47,270 --> 00:04:52,070
use these Boolean values in if checks, that is important.

87
00:04:52,070 --> 00:04:55,460
Because again, that's also a certain way of working

88
00:04:55,460 --> 00:04:57,890
with Booleans and if statements,

89
00:04:57,890 --> 00:05:01,083
which you will see a lot in web development.

