Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
WEBVTT
1
00:00:00.000 --> 00:00:04.000
In the last lecture
2
00:00:04.000 --> 00:00:08.000
you learned about logical operators, in all the examples you saw
3
00:00:08.000 --> 00:00:12.000
in the last lecture, we used these logical operators with boolean
4
00:00:12.000 --> 00:00:16.000
values. But in JavaScript, unlike in many programming languages,
5
00:00:16.000 --> 00:00:20.000
we can use these logical operators with non boolean values.
6
00:00:20.000 --> 00:00:24.000
And that's extremely powerful. Let me show you a few examples.
7
00:00:24.000 --> 00:00:28.000
Let's say we have false or true. What do you
8
00:00:28.000 --> 00:00:32.000
think is the result of this expression? Well, it's true, right?
9
00:00:32.000 --> 00:00:36.000
Now what if we have false or
10
00:00:36.000 --> 00:00:40.000
the string Mosh? What do you think is the result of this
11
00:00:40.000 --> 00:00:44.000
expression? Well let's take a look, we get Mosh back
12
00:00:44.000 --> 00:00:48.000
what if we have false or number 1?
13
00:00:48.000 --> 00:00:52.000
We get number 1 back. So here's the first thing you
14
00:00:52.000 --> 00:00:56.000
need to realize, the result of a logical expression is not
15
00:00:56.000 --> 00:01:00.000
necessarily a true or false. That depends on
16
00:01:00.000 --> 00:01:04.000
the value of the operance we have. In the first example
17
00:01:04.000 --> 00:01:08.000
our second operand is true, that's why we get true back.
18
00:01:08.000 --> 00:01:12.000
In the second example, our second operand is a string, that's why we get
19
00:01:12.000 --> 00:01:16.000
a string back, and by the same token, in the third example.
20
00:01:16.000 --> 00:01:20.000
We get a number back. So when our javaScript engine tries to
21
00:01:20.000 --> 00:01:24.000
evalue this expression, it looks at each operand
22
00:01:24.000 --> 00:01:28.000
if that operand is not a boolean true or false, it will try
23
00:01:28.000 --> 00:01:32.000
to interpret it as truthy or
24
00:01:32.000 --> 00:01:36.000
falsey, so in JavaScript we have
25
00:01:36.000 --> 00:01:40.000
is values which we refer to as Falsey, that's not
26
00:01:40.000 --> 00:01:44.000
boolean false, it's falsey. What are
27
00:01:44.000 --> 00:01:48.000
these falsey values? Well we have undefined
28
00:01:48.000 --> 00:01:52.000
null, number 0, the boolean
29
00:01:52.000 --> 00:01:56.000
false, and empty string and
30
00:01:56.000 --> 00:02:00.000
not a number. Now we haven't talked about this yet, that's going to come
31
00:02:00.000 --> 00:02:04.000
later in the course, for now, just remember, not a number
32
00:02:04.000 --> 00:02:08.000
is a special value in JavaScript, and when we are dealing with a mathematical
33
00:02:08.000 --> 00:02:12.000
calculation that does not produce a valid number,
34
00:02:12.000 --> 00:02:16.000
this value is returned, not a number. Now we're going to look at that later in the course,
35
00:02:16.000 --> 00:02:20.000
so don't worry about it. So these are the falsey values
36
00:02:20.000 --> 00:02:24.000
in JavaScript, if we use any of these values in a logical expression
37
00:02:24.000 --> 00:02:28.000
they will be treated as falsey, which is kind of like
38
00:02:28.000 --> 00:02:32.000
a boolean false, but it's not exactly the same. Now,
39
00:02:32.000 --> 00:02:36.000
anything that is not falsey is truthy.
40
00:02:36.000 --> 00:02:40.000
So, back to these examples.
41
00:02:40.000 --> 00:02:44.000
In the second example, our second operand is a string with
42
00:02:44.000 --> 00:02:48.000
4 characters. So this is not an empty string, it's not
43
00:02:48.000 --> 00:02:52.000
falsey, so it's truthy. So when our JavaScript
44
00:02:52.000 --> 00:02:56.000
engine tries to evaluate this, it looks at the first operand
45
00:02:56.000 --> 00:03:00.000
it's false so the search continues because with a logical or
46
00:03:00.000 --> 00:03:04.000
operator, as you learned in the last lecture, as long as one of the operands
47
00:03:04.000 --> 00:03:08.000
is true the result will be true. Now here in the second
48
00:03:08.000 --> 00:03:12.000
example, the first operand is false, so the search continues
49
00:03:12.000 --> 00:03:16.000
hoping that maybe the other operand is true or
50
00:03:16.000 --> 00:03:20.000
truthy. In this case we are dealing with a truthy value, so
51
00:03:20.000 --> 00:03:24.000
this value is immediately returned. The same goes for
52
00:03:24.000 --> 00:03:28.000
third example. So here one is not a boolean true, it's truthy,
53
00:03:28.000 --> 00:03:32.000
that's why the value of this operand is returned.
54
00:03:32.000 --> 00:03:36.000
Now, what if we have an expression like this, false
55
00:03:36.000 --> 00:03:40.000
or 1, or 2. What do you think we're going to get?
56
00:03:40.000 --> 00:03:44.000
Let's take a look. You get 1. So this is another thing
57
00:03:44.000 --> 00:03:48.000
you need to understand about the logical or operator. The evaluation
58
00:03:48.000 --> 00:03:52.000
starts here, as soon as we find an operand that is
59
00:03:52.000 --> 00:03:56.000
truthy, that operand is returned, so here our second operand is
60
00:03:56.000 --> 00:04:00.000
truthy, it's value is returned, and here the evaluation stops.
61
00:04:00.000 --> 00:04:04.000
It doesn't matter what we have on the right side, we could have a million other operands,
62
00:04:04.000 --> 00:04:08.000
they are completely ignored, this is what we call
63
00:04:08.000 --> 00:04:12.000
short-circuiting. Short-circuiting. That's
64
00:04:12.000 --> 00:04:16.000
exactly like short circuiting that we have in electricity.
65
00:04:16.000 --> 00:04:20.000
Now that you understand how these logical operators work with non-boolean values,
66
00:04:20.000 --> 00:04:24.000
let me give you a real world example on when to use this. So
67
00:04:24.000 --> 00:04:28.000
let's imagine an application, and
68
00:04:28.000 --> 00:04:32.000
somewhere the user has to pick a color, or we're going to use a default
69
00:04:32.000 --> 00:04:36.000
color. Maybe this is the color of the t-shirt they want to buy. So,
70
00:04:36.000 --> 00:04:40.000
I'm going to declare a few variables, userColor.
71
00:04:40.000 --> 00:04:44.000
We set that to red, defaultColor
72
00:04:44.000 --> 00:04:48.000
we set that to blue, and currentColor,
73
00:04:48.000 --> 00:04:52.000
which we set to userColor or
74
00:04:52.000 --> 00:04:56.000
defaultColor. Now let's read this exprsssion
75
00:04:56.000 --> 00:05:00.000
in plain English. userColor or defaultColor
76
00:05:00.000 --> 00:05:04.000
that basically means if we have a value for userColor
77
00:05:04.000 --> 00:05:08.000
we're going to use that, if we don't, we're going to use the defaultColor.
78
00:05:08.000 --> 00:05:12.000
Now, let's log this on the console. So currentColor
79
00:05:12.000 --> 00:05:16.000
we get red. Because our user
80
00:05:16.000 --> 00:05:20.000
has selected a color. In contrast, if the user
81
00:05:20.000 --> 00:05:24.000
has not selected a color, let's set this to undefined,
82
00:05:24.000 --> 00:05:28.000
save the changes, we get blue.
83
00:05:28.000 --> 00:05:32.000
Because defaultColor is blue. So this is the power
84
00:05:32.000 --> 00:05:36.000
of using the logical or operator, between nonbooleans.
85
00:05:36.000 --> 00:05:40.000
With this technique, we can provide default values.
86
00:05:40.000 --> 00:05:44.000
So if the user has selected a color, we're going to use that
87
00:05:44.000 --> 00:05:48.000
otherwise we're going to use our default color and that determines
88
00:05:48.000 --> 00:05:52.000
the current color which we are going to display on the screen.
7727
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.