1
00:00:09,220 --> 00:00:10,840
Hey, everybody, welcome back.

2
00:00:10,900 --> 00:00:17,950
In this video, we are going to talk about booleans, this data type is very important, not only in

3
00:00:17,970 --> 00:00:21,160
JavaScript but in any programming language.

4
00:00:21,490 --> 00:00:28,870
Booleans are behind all the logic of our programs and they represent the true and false values.

5
00:00:29,260 --> 00:00:32,860
So if I create a new variable here called bool.

6
00:00:34,250 --> 00:00:40,820
I'm going to assign a value of true to it and then let me try to type off.

7
00:00:40,880 --> 00:00:42,860
Operator of my variable.

8
00:00:43,200 --> 00:00:50,120
And now, as you can see, it's telling us that this variable has a value of the type Boulin.

9
00:00:50,360 --> 00:00:54,980
So we have to and we also have the false value.

10
00:00:55,250 --> 00:00:59,120
Both of them are written like this without quotation marks.

11
00:00:59,390 --> 00:01:01,430
And in lower case.

12
00:01:01,940 --> 00:01:03,080
So let's try it again.

13
00:01:03,350 --> 00:01:04,730
Let's try to type off again.

14
00:01:05,090 --> 00:01:07,580
And we can see that it's also a boolean.

15
00:01:07,820 --> 00:01:13,700
So what's the use of assigning a true or false value to a variable?

16
00:01:14,120 --> 00:01:15,680
Actually, not so much.

17
00:01:15,950 --> 00:01:18,980
But this is not how we are going to use booleans.

18
00:01:19,310 --> 00:01:25,640
Actually, boolean values are going to be produced by comparisons.

19
00:01:25,850 --> 00:01:27,800
So we are going to compare things.

20
00:01:28,100 --> 00:01:31,220
Is this thing the same as this other thing?

21
00:01:31,490 --> 00:01:35,120
Is this number higher than this other number?

22
00:01:35,330 --> 00:01:41,330
And then these comparisons are going to produce a true or false value.

23
00:01:42,290 --> 00:01:45,530
So here we have the comparison operators.

24
00:01:45,830 --> 00:01:51,920
So we have greater than greater than or equal to let's try these two to get started.

25
00:01:52,280 --> 00:02:00,920
So if I ask JavaScript if instead of comparing two numbers, because maybe that won't make any sense.

26
00:02:01,820 --> 00:02:04,880
Let's create a variable for the age of a person.

27
00:02:06,240 --> 00:02:14,520
So let's say the person is 16 and I'll maybe we want to ask JavaScript if this person is over 18.

28
00:02:14,820 --> 00:02:21,340
So we're going to ask if age is greater than or equal to 18 and out.

29
00:02:21,420 --> 00:02:22,860
JavaScript is stalling us.

30
00:02:22,890 --> 00:02:24,690
No, no, this is false.

31
00:02:25,380 --> 00:02:27,660
This person is not over 18.

32
00:02:27,690 --> 00:02:30,810
So this is how we are going to produce these values.

33
00:02:30,990 --> 00:02:33,230
So now let's say the person is 18.

34
00:02:35,330 --> 00:02:37,190
And we do the comparison again.

35
00:02:37,460 --> 00:02:40,130
And now it's telling us that this is true.

36
00:02:40,310 --> 00:02:42,290
Now we could do the opposite.

37
00:02:42,500 --> 00:02:51,380
So instead of asking if the age is greater than 18, I could ask if the age is less than 18.

38
00:02:51,620 --> 00:02:56,450
So I could ask let me just assign it again just to make sure.

39
00:02:56,480 --> 00:02:58,570
So now let's try 19.

40
00:02:59,330 --> 00:03:05,730
Let's say only people under 18 could participate in a competition or something like this.

41
00:03:05,750 --> 00:03:12,620
Maybe we have an inscription page that will only accept people younger than 18.

42
00:03:12,830 --> 00:03:18,080
So we could ask, is this age less than 18?

43
00:03:18,920 --> 00:03:19,970
Then it's false.

44
00:03:20,060 --> 00:03:23,900
So we won't allow the person to do the inscription.

45
00:03:25,410 --> 00:03:28,890
Then we also have the equal to operator.

46
00:03:28,980 --> 00:03:32,270
So let's say we have those password feud's.

47
00:03:32,670 --> 00:03:36,090
So let's just create a variable called password.

48
00:03:39,810 --> 00:03:42,740
And now let's just try one, two, three, four.

49
00:03:43,790 --> 00:03:45,440
A or something like this.

50
00:03:46,560 --> 00:03:47,830
A terrible password.

51
00:03:47,860 --> 00:03:49,510
But it's just for this example.

52
00:03:49,540 --> 00:03:54,380
Now, let's say we have another feud asking for the person to confirm the password.

53
00:03:54,430 --> 00:03:55,870
So typing it again.

54
00:03:56,350 --> 00:03:59,740
So this is going to be stored in a variable called confirm.

55
00:04:00,780 --> 00:04:01,410
Password.

56
00:04:03,290 --> 00:04:08,410
Now, this time, the person made a mistake and just wrote one, two, three, eight.

57
00:04:08,590 --> 00:04:12,560
The person forgot to Type four for now.

58
00:04:12,590 --> 00:04:18,260
We are going to compare if the passwords match so we could do like is password.

59
00:04:20,490 --> 00:04:27,160
The same as confirm password, and our JavaScript is stalling us that No.

60
00:04:27,390 --> 00:04:28,770
This is not the same thing.

61
00:04:28,800 --> 00:04:32,250
So we can't let the person proceed.

62
00:04:32,730 --> 00:04:35,610
Now, let's try to change this variable.

63
00:04:36,450 --> 00:04:39,510
So now confirm password is going to be the same.

64
00:04:40,350 --> 00:04:42,050
So now, one, two, three, four, a..

65
00:04:42,450 --> 00:04:44,090
Now doing the comparison again.

66
00:04:44,310 --> 00:04:45,000
This is true.

67
00:04:45,000 --> 00:04:46,260
So we can go ahead.

68
00:04:48,590 --> 00:04:56,300
When dealing with comparisons, there's also the identical operator, which could be useful sometimes

69
00:04:56,300 --> 00:05:05,300
because if you try to do like four as a string and if you ask JavaScript if this is the same as for

70
00:05:05,630 --> 00:05:07,910
it is going to tell us that this is true.

71
00:05:08,180 --> 00:05:14,330
But depending on the situation, this is not exactly true because for as a number is not the same as

72
00:05:14,330 --> 00:05:16,110
for as a string.

73
00:05:16,130 --> 00:05:18,620
This could be like part of a phone number.

74
00:05:18,860 --> 00:05:21,380
While this is a real number.

75
00:05:21,710 --> 00:05:30,650
So when we use the identical operator, this is only going to return true if both value and type are

76
00:05:30,650 --> 00:05:31,160
the same.

77
00:05:31,460 --> 00:05:37,100
So if we try to do it with the identical operator now, the result is false.

78
00:05:37,610 --> 00:05:40,970
So now let's try again using two strings.

79
00:05:41,420 --> 00:05:43,430
And now we can see that this is true.

80
00:05:43,760 --> 00:05:47,360
Another cool operator is the not equal two.

81
00:05:47,360 --> 00:05:53,390
So this is represented by the exclamation mark, actually exclamation marks.

82
00:05:53,870 --> 00:05:58,090
They normally in most programming languages, they mean not.

83
00:05:58,460 --> 00:06:01,970
So they are going to be the opposite of what you are trying to do.

84
00:06:02,390 --> 00:06:04,850
So let's try this comparison again.

85
00:06:05,920 --> 00:06:11,440
So I'm asking JavaScript if for let's use two numbers just to make it easier.

86
00:06:11,800 --> 00:06:16,690
I'm asking JavaScript if four is the same as four.

87
00:06:17,140 --> 00:06:19,000
And it's telling me that this is true.

88
00:06:19,030 --> 00:06:23,530
But if I use the exclamation mark, I'm asking the opposite.

89
00:06:23,890 --> 00:06:25,360
I'm asking JavaScript.

90
00:06:25,660 --> 00:06:28,930
Is this thing not the same as this thing?

91
00:06:29,380 --> 00:06:33,970
Well, this statement is false because those are actually the same thing.

92
00:06:35,080 --> 00:06:39,170
So I don't want to overcomplicate things with this operator now.

93
00:06:39,340 --> 00:06:40,750
I just wanted to show you.

94
00:06:40,770 --> 00:06:44,470
And then in due time, we are going to start using it.

95
00:06:44,710 --> 00:06:45,090
All right.

96
00:06:45,100 --> 00:06:48,580
So now we can see that booleans can be very useful.

97
00:06:48,670 --> 00:06:54,860
I just showed a few examples, like the password example of RDA age example.

98
00:06:55,420 --> 00:07:01,600
But normally we are not going to use comparison operators isolated like this.

99
00:07:01,900 --> 00:07:06,070
Normally we are going to use them inside conditionals.

100
00:07:06,460 --> 00:07:08,560
So I have an example here to show you.

101
00:07:09,740 --> 00:07:12,890
Let's go to Visual Studio code to the script.

102
00:07:12,920 --> 00:07:14,630
Dot J.S. file.

103
00:07:14,870 --> 00:07:20,120
So later in the course, we're going to have a lesson to talk more about conditionals.

104
00:07:20,120 --> 00:07:22,250
So this is just an introduction.

105
00:07:22,370 --> 00:07:28,970
I just wanted to show you this because it's impossible to talk about booleans and don't show you an

106
00:07:28,970 --> 00:07:30,520
example of a conditional.

107
00:07:30,830 --> 00:07:41,000
So let's say on our Web site, we offer finance options if the order value is hired and a thousand dollars.

108
00:07:41,270 --> 00:07:46,400
So if the person is buying two thousand dollars, we can offer finance options.

109
00:07:46,460 --> 00:07:52,040
If it's lowered and a thousand dollars, then we cannot finance the order.

110
00:07:52,190 --> 00:07:55,730
So how would we do this with booleans and conditioner's?

111
00:07:56,090 --> 00:07:59,540
Well, first, let's create a variable called order value.

112
00:08:00,670 --> 00:08:03,910
Again, we are going to assign it ourselves.

113
00:08:03,940 --> 00:08:06,300
But normally this is not what happens.

114
00:08:06,700 --> 00:08:10,390
The person will choose a product that will have a value.

115
00:08:10,630 --> 00:08:13,960
So then this value is going to be assigned for the variable.

116
00:08:14,380 --> 00:08:19,930
But right now, just for the sake of our example, let's assign to value ourselves.

117
00:08:20,230 --> 00:08:24,370
So let's say the order value is two thousand eight hundred.

118
00:08:25,240 --> 00:08:26,860
So this is above a thousand.

119
00:08:27,400 --> 00:08:31,960
This person is eligible for financing the order.

120
00:08:32,350 --> 00:08:36,790
So to do a conditional, we are going to use the if statement.

121
00:08:37,570 --> 00:08:39,490
So we are going to tell JavaScript.

122
00:08:39,700 --> 00:08:43,990
If the order value is.

123
00:08:45,630 --> 00:08:51,720
Greater than or equal to 1000, then we want to do something.

124
00:08:51,990 --> 00:08:56,120
So this then is going to be inside curly braces.

125
00:08:56,580 --> 00:08:58,410
So this is how we write conditions.

126
00:08:58,890 --> 00:09:08,370
So if this thing here, this comparison returns true, then we want to go inside here and do anything

127
00:09:08,400 --> 00:09:08,970
we want.

128
00:09:09,120 --> 00:09:11,820
We can write multiple statements inside here.

129
00:09:12,330 --> 00:09:17,430
In this case, just for our example, I'm just gonna type console dot log.

130
00:09:20,340 --> 00:09:22,210
And I'm going to write you are?

131
00:09:23,770 --> 00:09:24,520
Eligible.

132
00:09:25,710 --> 00:09:29,010
To finance your order.

133
00:09:29,610 --> 00:09:30,540
Let's do this.

134
00:09:30,840 --> 00:09:31,950
Let's go back there.

135
00:09:32,520 --> 00:09:33,990
Let's refresh the page.

136
00:09:34,230 --> 00:09:38,520
And now we can see that we are seeing the message in the console.

137
00:09:39,030 --> 00:09:43,260
On the other hand, if we had an order value of eight hundred.

138
00:09:45,430 --> 00:09:47,560
Now we are not seeing anything.

139
00:09:48,280 --> 00:09:52,360
So there is one more thing that you can do if this is not true.

140
00:09:52,690 --> 00:09:57,790
You can choose what's going to happen otherwise, so you can use the else statement.

141
00:09:59,010 --> 00:10:04,830
So if this is true, do this, otherwise do what's inside the else statement.

142
00:10:05,190 --> 00:10:09,270
So in this case, I'm going to write console that log.

143
00:10:11,620 --> 00:10:13,080
And I'm just going to type.

144
00:10:13,860 --> 00:10:14,670
Finance.

145
00:10:15,710 --> 00:10:16,520
Options.

146
00:10:20,530 --> 00:10:24,020
Are not available for orders below.

147
00:10:27,870 --> 00:10:29,250
One thousand dollars.

148
00:10:29,490 --> 00:10:33,240
So by doing this now, we have the order value of eight hundred.

149
00:10:34,530 --> 00:10:40,860
If we refresh the page, we can see that the finance options are not available.

150
00:10:41,700 --> 00:10:46,200
But if we have something like twelve hundred.

151
00:10:49,470 --> 00:10:50,700
Refresh the page.

152
00:10:50,730 --> 00:10:54,600
Now we have finance options to choose from.

153
00:10:54,870 --> 00:10:57,480
So I just wanted to show you this school example.

154
00:10:57,510 --> 00:10:59,940
This is a real life situation.

155
00:10:59,970 --> 00:11:06,420
This is something that we do on Web sites all the time, comparing values to see if we can offer Fang's.

156
00:11:06,420 --> 00:11:10,920
You could offer like free shipping on orders above a hundred dollars.

157
00:11:11,340 --> 00:11:13,770
So this is really a common thing to do.

158
00:11:14,010 --> 00:11:16,710
There's more that I need to talk about conditionals.

159
00:11:16,740 --> 00:11:20,760
But this is going to be done in a separate lesson for now.

160
00:11:20,790 --> 00:11:22,530
That was all that I wanted to say.

161
00:11:22,950 --> 00:11:24,540
So I'll see you in the next video.
