1
00:00:01,310 --> 00:00:03,780
<v Instructor>When we learn about if-else statements,</v>

2
00:00:03,780 --> 00:00:06,290
comparison and equality operators

3
00:00:06,290 --> 00:00:07,910
and Boolean values.

4
00:00:07,910 --> 00:00:10,630
We also need to learn about logic.

5
00:00:10,630 --> 00:00:13,710
And in particular about Boolean logic.

6
00:00:13,710 --> 00:00:16,133
So let's do that in this video.

7
00:00:17,580 --> 00:00:20,090
So basically Boolean logic

8
00:00:20,090 --> 00:00:22,220
is a branch of computer science,

9
00:00:22,220 --> 00:00:24,430
which uses true and false values

10
00:00:24,430 --> 00:00:27,340
to solve complex logical problems.

11
00:00:27,340 --> 00:00:28,820
And in order to do that,

12
00:00:28,820 --> 00:00:31,200
it uses several logical operators

13
00:00:31,200 --> 00:00:34,130
to combine true and false values.

14
00:00:34,130 --> 00:00:36,680
So much like we use arithmetic operators,

15
00:00:36,680 --> 00:00:38,990
to combine numeric values.

16
00:00:38,990 --> 00:00:41,530
Now we're only going to scratch the surface here

17
00:00:41,530 --> 00:00:44,840
and talk about the most basic logical operators,

18
00:00:44,840 --> 00:00:48,480
which are the AND, OR, and NOT operators.

19
00:00:48,480 --> 00:00:50,490
And note that Boolean logic

20
00:00:50,490 --> 00:00:53,670
is actually not specific to JavaScript.

21
00:00:53,670 --> 00:00:56,670
This is all true for all programming.

22
00:00:56,670 --> 00:00:57,503
But anyway,

23
00:00:57,503 --> 00:01:00,260
let's now understand how these operators work

24
00:01:00,260 --> 00:01:03,710
and use an example to illustrate it.

25
00:01:03,710 --> 00:01:06,460
So here we have two Boolean variables

26
00:01:06,460 --> 00:01:09,440
that can either be true or false.

27
00:01:09,440 --> 00:01:13,120
So A is that Sarah has a driver's license

28
00:01:13,120 --> 00:01:15,900
and B is that Sarah has good vision.

29
00:01:15,900 --> 00:01:18,520
So both of these are of course conditions,

30
00:01:18,520 --> 00:01:20,580
that can be true or false

31
00:01:20,580 --> 00:01:23,210
because Sarah could have no driver's license

32
00:01:23,210 --> 00:01:25,380
and she could have no good vision,

33
00:01:25,380 --> 00:01:26,340
right.

34
00:01:26,340 --> 00:01:28,070
And of course on the other hand,

35
00:01:28,070 --> 00:01:30,190
she could also have a driver's license

36
00:01:30,190 --> 00:01:32,620
and could have good vision.

37
00:01:32,620 --> 00:01:36,800
Now using the AND operator like A aND B,

38
00:01:36,800 --> 00:01:38,940
we can combine these two Boolean variables

39
00:01:38,940 --> 00:01:43,510
like Sarah has a driver's license and good vision.

40
00:01:43,510 --> 00:01:44,343
Okay.

41
00:01:44,343 --> 00:01:47,860
But now what will be the result of this operation?

42
00:01:47,860 --> 00:01:48,950
Well for that,

43
00:01:48,950 --> 00:01:51,370
we use a so-called truth table,

44
00:01:51,370 --> 00:01:53,880
which looks something like this.

45
00:01:53,880 --> 00:01:55,860
Here we have to two possible values

46
00:01:55,860 --> 00:01:57,230
for each of the variables,

47
00:01:57,230 --> 00:01:58,380
A AND B,

48
00:01:58,380 --> 00:02:02,580
which gives us four possible combinations of results.

49
00:02:02,580 --> 00:02:04,190
And so then all we have to do

50
00:02:04,190 --> 00:02:06,909
is to plug in the value of our variables,

51
00:02:06,909 --> 00:02:09,790
and to get a result of the AND operation

52
00:02:09,790 --> 00:02:11,290
from this table.

53
00:02:11,290 --> 00:02:13,740
So what we can see from this table

54
00:02:13,740 --> 00:02:17,150
is that only if both A and B are true,

55
00:02:17,150 --> 00:02:20,940
the result of the operation will be true as well.

56
00:02:20,940 --> 00:02:23,060
And that makes sense, right!

57
00:02:23,060 --> 00:02:23,950
After all,

58
00:02:23,950 --> 00:02:26,260
it's called the AND operator.

59
00:02:26,260 --> 00:02:27,310
And so again,

60
00:02:27,310 --> 00:02:29,190
what the AND operator does

61
00:02:29,190 --> 00:02:30,850
is to return true

62
00:02:30,850 --> 00:02:34,320
only if both A AND B are true.

63
00:02:34,320 --> 00:02:35,910
In all other situations,

64
00:02:35,910 --> 00:02:38,730
if either A or B are false,

65
00:02:38,730 --> 00:02:41,750
then A AND B will also be false.

66
00:02:41,750 --> 00:02:44,680
And we can also generalize this to more operands.

67
00:02:44,680 --> 00:02:47,150
So to more than just two values,

68
00:02:47,150 --> 00:02:49,870
we could do A AND B AND C,

69
00:02:49,870 --> 00:02:51,430
and results would be true

70
00:02:51,430 --> 00:02:54,320
only if all of them are true.

71
00:02:54,320 --> 00:02:55,920
And if one of them is false,

72
00:02:55,920 --> 00:02:59,870
then the result is immediately false as well.

73
00:02:59,870 --> 00:03:01,680
Now I know that all of this,

74
00:03:01,680 --> 00:03:04,360
might look a little bit confusing,

75
00:03:04,360 --> 00:03:06,700
but don't worry We will have an example,

76
00:03:06,700 --> 00:03:08,710
a little bit later in this lecture,

77
00:03:08,710 --> 00:03:10,830
and we will also use this in practice

78
00:03:10,830 --> 00:03:12,140
and the next one.

79
00:03:12,140 --> 00:03:13,210
And after some practice,

80
00:03:13,210 --> 00:03:15,340
you will internalize all of this

81
00:03:15,340 --> 00:03:18,239
and you won't even need any truth tables.

82
00:03:18,239 --> 00:03:19,710
Now moving on,

83
00:03:19,710 --> 00:03:20,599
on the other hand,

84
00:03:20,599 --> 00:03:23,670
we also have the OR operator,

85
00:03:23,670 --> 00:03:26,380
which kind of works in the opposite way.

86
00:03:26,380 --> 00:03:28,210
So with our current example,

87
00:03:28,210 --> 00:03:29,970
we could use the OR operator

88
00:03:29,970 --> 00:03:31,820
to determine whether Sarah

89
00:03:31,820 --> 00:03:35,000
has a driver's license or good vision.

90
00:03:35,000 --> 00:03:37,120
And just by thinking about this,

91
00:03:37,120 --> 00:03:39,620
we can already intuitively imagine

92
00:03:39,620 --> 00:03:41,960
that the OR operator will be true

93
00:03:41,960 --> 00:03:44,710
if just one of the variables is true.

94
00:03:44,710 --> 00:03:45,640
And in fact,

95
00:03:45,640 --> 00:03:50,170
a look at the truth table confirms exactly that.

96
00:03:50,170 --> 00:03:53,850
So unlike the AND operator and the OR operator,

97
00:03:53,850 --> 00:03:56,400
even if one of the variables is false,

98
00:03:56,400 --> 00:03:59,130
the outcome will still be true.

99
00:03:59,130 --> 00:04:00,380
Or in other words,

100
00:04:00,380 --> 00:04:02,830
if either A OR B is true,

101
00:04:02,830 --> 00:04:05,780
then the OR operation becomes true as well.

102
00:04:05,780 --> 00:04:08,900
And that's why it's called the OR operator.

103
00:04:08,900 --> 00:04:10,230
And once more,

104
00:04:10,230 --> 00:04:12,260
if we have multiple variables,

105
00:04:12,260 --> 00:04:15,010
it's enough for one of them to be true,

106
00:04:15,010 --> 00:04:18,360
to make the whole operation become true as well.

107
00:04:18,360 --> 00:04:20,660
Okay, I hope this makes sense.

108
00:04:20,660 --> 00:04:21,810
And once again,

109
00:04:21,810 --> 00:04:22,980
I can assure you

110
00:04:22,980 --> 00:04:24,480
that it will make sense

111
00:04:24,480 --> 00:04:27,840
once we start using this in practice.

112
00:04:27,840 --> 00:04:28,673
Anyway,

113
00:04:28,673 --> 00:04:31,830
we finally also have the NOT operator,

114
00:04:31,830 --> 00:04:33,360
which is a lot simpler

115
00:04:33,360 --> 00:04:36,060
because it doesn't combine multiple values.

116
00:04:36,060 --> 00:04:36,990
Instead,

117
00:04:36,990 --> 00:04:40,920
the NOT operator acts on only one Boolean value

118
00:04:40,920 --> 00:04:43,420
and it basically just inverts it.

119
00:04:43,420 --> 00:04:44,910
So if A is true,

120
00:04:44,910 --> 00:04:46,270
it will become false.

121
00:04:46,270 --> 00:04:47,400
And if it's false,

122
00:04:47,400 --> 00:04:49,720
then NOT A will become true,

123
00:04:49,720 --> 00:04:51,400
simple as that.

124
00:04:51,400 --> 00:04:52,320
And that's basically

125
00:04:52,320 --> 00:04:56,200
how these three most important logical operators work.

126
00:04:56,200 --> 00:04:57,033
But of course,

127
00:04:57,033 --> 00:04:58,670
the best way to understand it

128
00:04:58,670 --> 00:05:01,640
is to use a more practical example.

129
00:05:01,640 --> 00:05:03,280
So let's go again back

130
00:05:03,280 --> 00:05:04,372
to using the age,

131
00:05:04,372 --> 00:05:07,600
and this time we set it to 16.

132
00:05:07,600 --> 00:05:08,737
And then we have,

133
00:05:08,737 --> 00:05:10,800
our two Boolean variables.

134
00:05:10,800 --> 00:05:14,630
The first one is age is greater or equal 20.

135
00:05:14,630 --> 00:05:18,660
And the second one is age is less than 30.

136
00:05:18,660 --> 00:05:21,580
So let's determine the value of each of them.

137
00:05:21,580 --> 00:05:23,072
So if age is 16,

138
00:05:23,072 --> 00:05:26,021
then age is greater or equal to any

139
00:05:26,021 --> 00:05:27,611
must be false.

140
00:05:27,611 --> 00:05:28,462
Right!

141
00:05:28,462 --> 00:05:29,950
And of course it is,

142
00:05:29,950 --> 00:05:32,533
and age is less than 30 is true

143
00:05:33,735 --> 00:05:37,642
because 16 obviously is less than 30.

144
00:05:37,642 --> 00:05:38,664
Okay.

145
00:05:38,664 --> 00:05:40,390
So that's the baseline basically,

146
00:05:40,390 --> 00:05:42,330
and now we will start to combine

147
00:05:42,330 --> 00:05:44,110
these variables A and B

148
00:05:44,110 --> 00:05:46,760
using some logical operators.

149
00:05:46,760 --> 00:05:48,630
So the first exercise

150
00:05:48,630 --> 00:05:50,350
is the NOT operator.

151
00:05:50,350 --> 00:05:51,510
So NOT A,

152
00:05:51,510 --> 00:05:56,070
and we know right now that A is false, right?

153
00:05:56,070 --> 00:05:57,970
So the result of NOT A,

154
00:05:57,970 --> 00:06:00,610
as we just learned is true,

155
00:06:00,610 --> 00:06:01,650
because all it does

156
00:06:01,650 --> 00:06:05,570
is to invert the logical value of the variable.

157
00:06:05,570 --> 00:06:08,190
Next one we have A AND B,

158
00:06:08,190 --> 00:06:12,060
now we already know that A is false and B is true.

159
00:06:12,060 --> 00:06:14,010
So I'm just putting these values here again,

160
00:06:14,010 --> 00:06:16,100
to make it a bit easier to follow.

161
00:06:16,100 --> 00:06:17,010
Okay.

162
00:06:17,010 --> 00:06:20,200
So we have basically false and true.

163
00:06:20,200 --> 00:06:22,650
So from a look at our truth table here

164
00:06:22,650 --> 00:06:23,830
at the right side,

165
00:06:23,830 --> 00:06:25,130
we can easily determine

166
00:06:25,130 --> 00:06:28,600
that the result of this must be false.

167
00:06:28,600 --> 00:06:29,550
And so that's exactly

168
00:06:29,550 --> 00:06:31,020
what we learned before.

169
00:06:31,020 --> 00:06:32,890
So when we have the AND operator,

170
00:06:32,890 --> 00:06:36,000
it's enough for one of the operands to be false,

171
00:06:36,000 --> 00:06:39,392
to make the result of the operation also false.

172
00:06:39,392 --> 00:06:42,050
Now we have A OR B,

173
00:06:42,050 --> 00:06:46,010
and so let's check out the truth table in this case.

174
00:06:46,010 --> 00:06:47,740
And so in this case,

175
00:06:47,740 --> 00:06:49,920
we actually get true.

176
00:06:49,920 --> 00:06:52,380
And so this shows that it is enough

177
00:06:52,380 --> 00:06:54,600
for one variable to be true

178
00:06:54,600 --> 00:06:57,023
for the whole expression to be true as well.

179
00:06:58,620 --> 00:07:02,210
Next up let's check NOT A AND B.

180
00:07:02,210 --> 00:07:03,430
And we already know

181
00:07:03,430 --> 00:07:07,300
that NOT A is true and B is true.

182
00:07:07,300 --> 00:07:09,170
And so at this time for the first time,

183
00:07:09,170 --> 00:07:12,260
we are combining multiple operators here

184
00:07:12,260 --> 00:07:16,030
and yeah we already know that true and true

185
00:07:16,030 --> 00:07:18,900
will of course be true as well.

186
00:07:18,900 --> 00:07:22,560
And now another one combining two operators,

187
00:07:22,560 --> 00:07:25,640
so A OR NOT B.

188
00:07:25,640 --> 00:07:27,200
And just as a side note,

189
00:07:27,200 --> 00:07:30,570
the NOT operator actually has proceedings

190
00:07:30,570 --> 00:07:33,400
over the OR and AND operators.

191
00:07:33,400 --> 00:07:36,510
So basically the values are inverted first

192
00:07:36,510 --> 00:07:40,920
and only then they are combined using AND or OR.

193
00:07:40,920 --> 00:07:45,920
So we know that A is false and B is true.

194
00:07:46,190 --> 00:07:49,360
So NOT B must be false, right

195
00:07:49,360 --> 00:07:53,880
then false OR false is also false.

196
00:07:53,880 --> 00:07:56,170
And actually that's the only way in which

197
00:07:56,170 --> 00:07:58,270
the OR operator can be false.

198
00:07:58,270 --> 00:08:00,343
If all of them are false basically.

199
00:08:01,450 --> 00:08:03,990
So I hope that made sense to you.

200
00:08:03,990 --> 00:08:05,720
And so in the next lecture,

201
00:08:05,720 --> 00:08:09,700
we will actually use these operators in a code.

202
00:08:09,700 --> 00:08:13,290
And then it will make even more sense to you.

203
00:08:13,290 --> 00:08:14,420
So by then,

204
00:08:14,420 --> 00:08:18,310
we probably won't even need the truth tables anymore,

205
00:08:18,310 --> 00:08:20,550
because it will become quite intuitive

206
00:08:20,550 --> 00:08:23,930
how both of these operators actually work.

207
00:08:23,930 --> 00:08:25,870
But anyway, let's now move on

208
00:08:25,870 --> 00:08:27,873
and I see you in the next video.

