Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
When we learn about if-else statements, 2
2
comparison and equality operators 3
3
and Boolean values. 4
4
We also need to learn about logic. 5
5
And in particular about Boolean logic. 6
6
So let's do that in this video. 7
7
So basically Boolean logic 8
8
is a branch of computer science, 9
9
which uses true and false values 10
10
to solve complex logical problems. 11
11
And in order to do that, 12
12
it uses several logical operators 13
13
to combine true and false values. 14
14
So much like we use arithmetic operators, 15
15
to combine numeric values. 16
16
Now we're only going to scratch the surface here 17
17
and talk about the most basic logical operators, 18
18
which are the AND, OR, and NOT operators. 19
19
And note that Boolean logic 20
20
is actually not specific to JavaScript. 21
21
This is all true for all programming. 22
22
But anyway, 23
23
let's now understand how these operators work 24
24
and use an example to illustrate it. 25
25
So here we have two Boolean variables 26
26
that can either be true or false. 27
27
So A is that Sarah has a driver's license 28
28
and B is that Sarah has good vision. 29
29
So both of these are of course conditions, 30
30
that can be true or false 31
31
because Sarah could have no driver's license 32
32
and she could have no good vision, 33
33
right. 34
34
And of course on the other hand, 35
35
she could also have a driver's license 36
36
and could have good vision. 37
37
Now using the AND operator like A aND B, 38
38
we can combine these two Boolean variables 39
39
like Sarah has a driver's license and good vision. 40
40
Okay. 41
41
But now what will be the result of this operation? 42
42
Well for that, 43
43
we use a so-called truth table, 44
44
which looks something like this. 45
45
Here we have to two possible values 46
46
for each of the variables, 47
47
A AND B, 48
48
which gives us four possible combinations of results. 49
49
And so then all we have to do 50
50
is to plug in the value of our variables, 51
51
and to get a result of the AND operation 52
52
from this table. 53
53
So what we can see from this table 54
54
is that only if both A and B are true, 55
55
the result of the operation will be true as well. 56
56
And that makes sense, right! 57
57
After all, 58
58
it's called the AND operator. 59
59
And so again, 60
60
what the AND operator does 61
61
is to return true 62
62
only if both A AND B are true. 63
63
In all other situations, 64
64
if either A or B are false, 65
65
then A AND B will also be false. 66
66
And we can also generalize this to more operands. 67
67
So to more than just two values, 68
68
we could do A AND B AND C, 69
69
and results would be true 70
70
only if all of them are true. 71
71
And if one of them is false, 72
72
then the result is immediately false as well. 73
73
Now I know that all of this, 74
74
might look a little bit confusing, 75
75
but don't worry We will have an example, 76
76
a little bit later in this lecture, 77
77
and we will also use this in practice 78
78
and the next one. 79
79
And after some practice, 80
80
you will internalize all of this 81
81
and you won't even need any truth tables. 82
82
Now moving on, 83
83
on the other hand, 84
84
we also have the OR operator, 85
85
which kind of works in the opposite way. 86
86
So with our current example, 87
87
we could use the OR operator 88
88
to determine whether Sarah 89
89
has a driver's license or good vision. 90
90
And just by thinking about this, 91
91
we can already intuitively imagine 92
92
that the OR operator will be true 93
93
if just one of the variables is true. 94
94
And in fact, 95
95
a look at the truth table confirms exactly that. 96
96
So unlike the AND operator and the OR operator, 97
97
even if one of the variables is false, 98
98
the outcome will still be true. 99
99
Or in other words, 100
100
if either A OR B is true, 101
101
then the OR operation becomes true as well. 102
102
And that's why it's called the OR operator. 103
103
And once more, 104
104
if we have multiple variables, 105
105
it's enough for one of them to be true, 106
106
to make the whole operation become true as well. 107
107
Okay, I hope this makes sense. 108
108
And once again, 109
109
I can assure you 110
110
that it will make sense 111
111
once we start using this in practice. 112
112
Anyway, 113
113
we finally also have the NOT operator, 114
114
which is a lot simpler 115
115
because it doesn't combine multiple values. 116
116
Instead, 117
117
the NOT operator acts on only one Boolean value 118
118
and it basically just inverts it. 119
119
So if A is true, 120
120
it will become false. 121
121
And if it's false, 122
122
then NOT A will become true, 123
123
simple as that. 124
124
And that's basically 125
125
how these three most important logical operators work. 126
126
But of course, 127
127
the best way to understand it 128
128
is to use a more practical example. 129
129
So let's go again back 130
130
to using the age, 131
131
and this time we set it to 16. 132
132
And then we have, 133
133
our two Boolean variables. 134
134
The first one is age is greater or equal 20. 135
135
And the second one is age is less than 30. 136
136
So let's determine the value of each of them. 137
137
So if age is 16, 138
138
then age is greater or equal to any 139
139
must be false. 140
140
Right! 141
141
And of course it is, 142
142
and age is less than 30 is true 143
143
because 16 obviously is less than 30. 144
144
Okay. 145
145
So that's the baseline basically, 146
146
and now we will start to combine 147
147
these variables A and B 148
148
using some logical operators. 149
149
So the first exercise 150
150
is the NOT operator. 151
151
So NOT A, 152
152
and we know right now that A is false, right? 153
153
So the result of NOT A, 154
154
as we just learned is true, 155
155
because all it does 156
156
is to invert the logical value of the variable. 157
157
Next one we have A AND B, 158
158
now we already know that A is false and B is true. 159
159
So I'm just putting these values here again, 160
160
to make it a bit easier to follow. 161
161
Okay. 162
162
So we have basically false and true. 163
163
So from a look at our truth table here 164
164
at the right side, 165
165
we can easily determine 166
166
that the result of this must be false. 167
167
And so that's exactly 168
168
what we learned before. 169
169
So when we have the AND operator, 170
170
it's enough for one of the operands to be false, 171
171
to make the result of the operation also false. 172
172
Now we have A OR B, 173
173
and so let's check out the truth table in this case. 174
174
And so in this case, 175
175
we actually get true. 176
176
And so this shows that it is enough 177
177
for one variable to be true 178
178
for the whole expression to be true as well. 179
179
Next up let's check NOT A AND B. 180
180
And we already know 181
181
that NOT A is true and B is true. 182
182
And so at this time for the first time, 183
183
we are combining multiple operators here 184
184
and yeah we already know that true and true 185
185
will of course be true as well. 186
186
And now another one combining two operators, 187
187
so A OR NOT B. 188
188
And just as a side note, 189
189
the NOT operator actually has proceedings 190
190
over the OR and AND operators. 191
191
So basically the values are inverted first 192
192
and only then they are combined using AND or OR. 193
193
So we know that A is false and B is true. 194
194
So NOT B must be false, right 195
195
then false OR false is also false. 196
196
And actually that's the only way in which 197
197
the OR operator can be false. 198
198
If all of them are false basically. 199
199
So I hope that made sense to you. 200
200
And so in the next lecture, 201
201
we will actually use these operators in a code. 202
202
And then it will make even more sense to you. 203
203
So by then, 204
204
we probably won't even need the truth tables anymore, 205
205
because it will become quite intuitive 206
206
how both of these operators actually work. 207
207
But anyway, let's now move on 208
208
and I see you in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.