Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,755 --> 00:00:02,588
In this video,
2
00:00:02,588 --> 00:00:05,260
we need to go back to value types.
3
00:00:05,260 --> 00:00:08,180
So types are one of the fundamental aspects
4
00:00:08,180 --> 00:00:11,720
in programming, and converting between types
5
00:00:11,720 --> 00:00:15,010
is something that we do in every programming language.
6
00:00:15,010 --> 00:00:17,820
For example, converting a string to a number
7
00:00:17,820 --> 00:00:19,680
or a number into a Boolean
8
00:00:19,680 --> 00:00:22,110
is something that we do all the time.
9
00:00:22,110 --> 00:00:24,610
And so it's important that we learn about this
10
00:00:24,610 --> 00:00:28,170
before being able to move on further in the course.
11
00:00:28,170 --> 00:00:31,920
That's especially true for a language like JavaScript,
12
00:00:31,920 --> 00:00:34,790
which sometimes behaves in a weird way,
13
00:00:34,790 --> 00:00:36,713
as we will see in this video.
14
00:00:38,200 --> 00:00:41,040
Now, in JavaScript, there is type conversion
15
00:00:41,040 --> 00:00:42,870
and type coercion.
16
00:00:42,870 --> 00:00:46,050
So they sound very similar but are different.
17
00:00:46,050 --> 00:00:49,370
So type conversion is when we manually convert
18
00:00:49,370 --> 00:00:51,640
from one type to another.
19
00:00:51,640 --> 00:00:53,340
One the other hand, type coercion
20
00:00:53,340 --> 00:00:56,540
is when JavaScript automatically converts types
21
00:00:56,540 --> 00:00:58,610
behind the scenes for us.
22
00:00:58,610 --> 00:01:01,070
So that's necessary in some situation
23
00:01:01,070 --> 00:01:04,710
but it happens implicitly, completely hidden from us.
24
00:01:04,710 --> 00:01:05,880
Okay?
25
00:01:05,880 --> 00:01:08,060
So let's start with type conversion,
26
00:01:08,060 --> 00:01:11,040
which remember, is when we explicitly want
27
00:01:11,040 --> 00:01:13,800
to convert from one type to another.
28
00:01:13,800 --> 00:01:16,120
So let's say that we have an input field
29
00:01:16,120 --> 00:01:20,240
on a web page for the user to input their birth year.
30
00:01:20,240 --> 00:01:24,593
And these inputs from input fields usually come as strings.
31
00:01:26,300 --> 00:01:30,460
So let's say that the inputYear that we get
32
00:01:30,460 --> 00:01:32,700
from the user interface
33
00:01:32,700 --> 00:01:36,240
is a string with the value 1991.
34
00:01:36,240 --> 00:01:39,490
But now if we want to do some calculations with this,
35
00:01:39,490 --> 00:01:41,480
this won't really work.
36
00:01:41,480 --> 00:01:42,830
So let me show that to you.
37
00:01:43,780 --> 00:01:45,113
So console.log.
38
00:01:46,120 --> 00:01:47,860
Let's say inputYear,
39
00:01:47,860 --> 00:01:50,700
and now plus 18.
40
00:01:50,700 --> 00:01:52,460
And so like this we can calculate
41
00:01:52,460 --> 00:01:56,250
in what year the person will become of full age.
42
00:01:56,250 --> 00:01:59,200
Now, remember that when we have a string
43
00:01:59,200 --> 00:02:01,370
and add something to the string,
44
00:02:01,370 --> 00:02:04,430
it will basically concatenate the strings.
45
00:02:04,430 --> 00:02:08,650
So we can't expect that this actually adds 18
46
00:02:08,650 --> 00:02:11,253
to this number here because it's a string.
47
00:02:12,260 --> 00:02:15,030
So let me show you what I mean with that.
48
00:02:15,030 --> 00:02:17,810
And so indeed, we know get the string,
49
00:02:17,810 --> 00:02:20,960
which basically contains the 1991 string
50
00:02:20,960 --> 00:02:22,933
and then the 18 as well.
51
00:02:24,536 --> 00:02:27,410
So we need a way of fixing this,
52
00:02:27,410 --> 00:02:28,990
which means that we need a way
53
00:02:28,990 --> 00:02:32,650
of converting this string to a number.
54
00:02:32,650 --> 00:02:35,363
So let me actually log that here first.
55
00:02:36,400 --> 00:02:38,260
So console.log.
56
00:02:38,260 --> 00:02:41,580
And then the way we convert this string to a number
57
00:02:41,580 --> 00:02:45,570
is by using the built-in Number function.
58
00:02:45,570 --> 00:02:47,270
So we write number,
59
00:02:47,270 --> 00:02:51,060
then parenthesis, and then inputYear.
60
00:02:51,060 --> 00:02:53,690
And we will learn exactly what a function is
61
00:02:53,690 --> 00:02:57,640
and why it works this way in the next section.
62
00:02:57,640 --> 00:03:01,230
For now, just know that we can basically convert strings
63
00:03:01,230 --> 00:03:04,730
to numbers by using this function,
64
00:03:04,730 --> 00:03:08,770
which we execute using these parenthesis here.
65
00:03:08,770 --> 00:03:11,970
So we have this parenthesis inside this parenthesis now
66
00:03:11,970 --> 00:03:13,883
but don't get confused by that.
67
00:03:14,890 --> 00:03:17,505
So doing this operation here
68
00:03:17,505 --> 00:03:20,773
will then return the string as a number.
69
00:03:21,630 --> 00:03:22,980
So let me show that to you.
70
00:03:24,200 --> 00:03:26,990
And so indeed, now we get 1991 here
71
00:03:26,990 --> 00:03:29,930
in this pink color basically.
72
00:03:29,930 --> 00:03:32,850
And the colors might change throughout the time.
73
00:03:32,850 --> 00:03:34,890
By the time I record this video,
74
00:03:34,890 --> 00:03:39,320
numbers are pink and strings are just this white.
75
00:03:39,320 --> 00:03:40,920
Let me actually print them both.
76
00:03:43,700 --> 00:03:46,810
And so that should make it really visible
77
00:03:46,810 --> 00:03:49,310
that yeah, the first one is a number,
78
00:03:49,310 --> 00:03:51,500
the second one is a string.
79
00:03:51,500 --> 00:03:54,730
Okay, but now one thing that's really important
80
00:03:54,730 --> 00:03:57,460
to note here is that the original value
81
00:03:57,460 --> 00:03:59,570
is actually not converted.
82
00:03:59,570 --> 00:04:02,720
So the inputYear variable itself,
83
00:04:02,720 --> 00:04:06,080
so this one, is still a string, right?
84
00:04:06,080 --> 00:04:08,870
It still holds the variable 1991
85
00:04:08,870 --> 00:04:11,440
as a string and not as a number.
86
00:04:11,440 --> 00:04:14,040
That's why down here in this log,
87
00:04:14,040 --> 00:04:15,980
where we do this calculation,
88
00:04:15,980 --> 00:04:18,220
the result is still this string
89
00:04:18,220 --> 00:04:21,420
because again, the original inputYear variable
90
00:04:21,420 --> 00:04:22,930
is still a string.
91
00:04:22,930 --> 00:04:24,350
Using this number function
92
00:04:24,350 --> 00:04:27,563
will simply give to us a converted version.
93
00:04:28,580 --> 00:04:31,296
So if you want to perform this calculation,
94
00:04:31,296 --> 00:04:35,410
we need to use Number here as well.
95
00:04:35,410 --> 00:04:37,920
And so this will now convert the number
96
00:04:37,920 --> 00:04:41,460
and then to that number, the 18 will be added.
97
00:04:41,460 --> 00:04:46,460
And so now we should end up with something like 2009 I guess
98
00:04:47,500 --> 00:04:49,970
and indeed, that's right.
99
00:04:49,970 --> 00:04:52,700
But now what if we're trying to convert something
100
00:04:52,700 --> 00:04:55,676
to a number that is impossible to convert?
101
00:04:55,676 --> 00:04:59,750
Let's try that with a string
102
00:04:59,750 --> 00:05:01,980
that doesn't really contain a number.
103
00:05:01,980 --> 00:05:05,410
So let's try to console.log converting
104
00:05:05,410 --> 00:05:09,110
to a number the string Jonas.
105
00:05:09,110 --> 00:05:11,360
So JavaScript will look at the string,
106
00:05:11,360 --> 00:05:13,810
will try to convert it to a number
107
00:05:13,810 --> 00:05:15,470
but it won't really work.
108
00:05:15,470 --> 00:05:17,153
So what do we get instead?
109
00:05:18,000 --> 00:05:21,870
We get this NaN, which stands for not a number.
110
00:05:21,870 --> 00:05:25,040
So JavaScript gives us this not a number value
111
00:05:25,040 --> 00:05:28,330
whenever an operation that involves numbers fails
112
00:05:28,330 --> 00:05:30,670
to produce a new number.
113
00:05:30,670 --> 00:05:35,210
So basically, not a number actually means invalid number.
114
00:05:35,210 --> 00:05:37,100
It's not really not a number.
115
00:05:37,100 --> 00:05:39,143
And let me actually prove that to you.
116
00:05:41,130 --> 00:05:43,217
So we can check the typeof NaN.
117
00:05:46,085 --> 00:05:47,418
So not a number.
118
00:05:48,360 --> 00:05:49,393
And as you will see,
119
00:05:50,420 --> 00:05:53,960
the weird result of this is that the type
120
00:05:53,960 --> 00:05:56,220
of not a number is actually number
121
00:05:56,220 --> 00:06:01,220
and so again, not a number actually means an invalid number.
122
00:06:01,520 --> 00:06:03,060
It's still a number somehow
123
00:06:03,060 --> 00:06:04,623
but it's an invalid one.
124
00:06:06,211 --> 00:06:07,700
And so again, we get not a number
125
00:06:07,700 --> 00:06:11,170
whenever an operation involving numbers fails
126
00:06:11,170 --> 00:06:13,810
to give us a new number.
127
00:06:13,810 --> 00:06:17,200
Okay, so that is converting strings to numbers
128
00:06:17,200 --> 00:06:19,733
but, of course, we can also do the opposite.
129
00:06:22,130 --> 00:06:25,280
It's a little bit less important I would say
130
00:06:25,280 --> 00:06:27,283
but I still want to show it to you.
131
00:06:28,970 --> 00:06:30,910
So to do it the other way around,
132
00:06:30,910 --> 00:06:33,430
we use this String function.
133
00:06:33,430 --> 00:06:36,790
And that's quite straightforward, right?
134
00:06:36,790 --> 00:06:39,560
Just keep in mind that we need to really start it
135
00:06:39,560 --> 00:06:43,330
with a capital S, just like here the Number function needs
136
00:06:43,330 --> 00:06:45,540
to start with a capital N.
137
00:06:45,540 --> 00:06:47,103
Otherwise it's not gonna work.
138
00:06:48,760 --> 00:06:50,950
So we get 23.
139
00:06:50,950 --> 00:06:54,130
And remember that whenever the value here is white,
140
00:06:54,130 --> 00:06:56,330
then it means it is a string.
141
00:06:56,330 --> 00:06:58,095
So it looks kind of the same.
142
00:06:58,095 --> 00:07:03,095
Let's again log both just to make this point.
143
00:07:04,120 --> 00:07:05,920
So the pink one is the value
144
00:07:05,920 --> 00:07:08,240
that actually has the number type
145
00:07:08,240 --> 00:07:10,353
and this one has the string type.
146
00:07:11,550 --> 00:07:14,540
Okay, so again, this one is not as important
147
00:07:14,540 --> 00:07:16,740
but I still wanted to mention it.
148
00:07:16,740 --> 00:07:20,570
Now, JavaScript can only convert to three types.
149
00:07:20,570 --> 00:07:24,000
So we can convert to a number, to a string
150
00:07:24,000 --> 00:07:25,950
or to a Boolean.
151
00:07:25,950 --> 00:07:28,660
But we cannot, for example, convert something
152
00:07:28,660 --> 00:07:31,200
to undefined or to null.
153
00:07:31,200 --> 00:07:33,410
That doesn't make a lot of sense.
154
00:07:33,410 --> 00:07:35,730
Now, here we only converted to numbers
155
00:07:35,730 --> 00:07:38,550
and to strings but not to Booleans.
156
00:07:38,550 --> 00:07:41,920
And that's because Booleans behave in a special way.
157
00:07:41,920 --> 00:07:44,870
And for that reason, there is a separate lecture coming up
158
00:07:44,870 --> 00:07:47,973
on so-called truthy and falsy values.
159
00:07:48,920 --> 00:07:51,160
Great, so that is type conversion
160
00:07:51,160 --> 00:07:52,900
where we do manually convert
161
00:07:52,900 --> 00:07:55,040
from one type to another.
162
00:07:55,040 --> 00:07:56,430
However, in practice,
163
00:07:56,430 --> 00:07:58,770
we rarely have to do it manually
164
00:07:58,770 --> 00:08:02,240
because JavaScript actually does type coercion automatically
165
00:08:02,240 --> 00:08:04,470
for us in many situations.
166
00:08:04,470 --> 00:08:06,440
So let's talk about that now.
167
00:08:06,440 --> 00:08:08,750
And let's just separate this here
168
00:08:08,750 --> 00:08:09,983
with some comments.
169
00:08:11,320 --> 00:08:12,913
So type conversion.
170
00:08:14,060 --> 00:08:14,893
And then
171
00:08:18,390 --> 00:08:20,310
type coercion.
172
00:08:20,310 --> 00:08:22,820
So basically, type coercion happens
173
00:08:22,820 --> 00:08:26,310
whenever an operator is dealing with two values
174
00:08:26,310 --> 00:08:27,990
that have different types.
175
00:08:27,990 --> 00:08:31,440
So in that case, JavaScript will then, behind the scenes,
176
00:08:31,440 --> 00:08:35,030
convert one of the values to match the other value
177
00:08:35,030 --> 00:08:35,950
so that in the end,
178
00:08:35,950 --> 00:08:38,220
the operation can be executed.
179
00:08:38,220 --> 00:08:40,750
And actually, we already saw that happening
180
00:08:40,750 --> 00:08:41,963
if you think about this.
181
00:08:42,950 --> 00:08:47,200
So let me show that to you,
182
00:08:47,200 --> 00:08:48,973
starting with strings.
183
00:08:49,910 --> 00:08:51,650
Remember how we did this.
184
00:08:51,650 --> 00:08:53,930
I am and then a number.
185
00:08:53,930 --> 00:08:55,323
Let's say 23.
186
00:08:56,950 --> 00:09:00,683
And then another plus years old.
187
00:09:01,570 --> 00:09:04,130
So we already know that this is gonna produce a string
188
00:09:04,130 --> 00:09:07,050
that says I am 23 years old.
189
00:09:07,050 --> 00:09:09,240
But how does that actually work?
190
00:09:09,240 --> 00:09:11,420
Because 23 is a number.
191
00:09:11,420 --> 00:09:13,940
So we have different types here, right?
192
00:09:13,940 --> 00:09:17,263
We have a string, a number and another string.
193
00:09:18,340 --> 00:09:23,110
So let's check and indeed, that is what happens.
194
00:09:23,110 --> 00:09:26,190
And it works this way because of type coercion.
195
00:09:26,190 --> 00:09:28,380
So in JavaScript, the plus operator
196
00:09:28,380 --> 00:09:32,470
that we used here triggers a coercion to strings.
197
00:09:32,470 --> 00:09:35,100
And so whenever there is an operation between a string
198
00:09:35,100 --> 00:09:39,540
and a number, the number will be converted to a string.
199
00:09:39,540 --> 00:09:41,160
So thanks to type coercion,
200
00:09:41,160 --> 00:09:43,470
writing this would be exactly the same
201
00:09:43,470 --> 00:09:45,803
as writing this.
202
00:09:50,510 --> 00:09:51,343
Right?
203
00:09:51,343 --> 00:09:53,040
Because again, the plus operator
204
00:09:53,040 --> 00:09:55,780
will convert numbers to strings.
205
00:09:55,780 --> 00:09:58,610
And the same actually happens in template literals.
206
00:09:58,610 --> 00:10:00,790
It also takes all the number values
207
00:10:00,790 --> 00:10:03,500
and also converts them to strings.
208
00:10:03,500 --> 00:10:06,900
Now, if JavaScript did not have automatic type coercion,
209
00:10:06,900 --> 00:10:08,879
like many other languages don't,
210
00:10:08,879 --> 00:10:11,730
then we would have to manually do this
211
00:10:11,730 --> 00:10:13,890
like we just learned before.
212
00:10:13,890 --> 00:10:17,870
Then we would have to do String 23
213
00:10:17,870 --> 00:10:21,220
and then this would be the only way that this would work.
214
00:10:21,220 --> 00:10:22,560
But luckily for us,
215
00:10:22,560 --> 00:10:24,380
JavaScript has type coercion
216
00:10:24,380 --> 00:10:27,080
and so this will happen completely automatically
217
00:10:27,080 --> 00:10:28,680
behind the scenes.
218
00:10:28,680 --> 00:10:31,320
Now, actually not all the operators
219
00:10:31,320 --> 00:10:33,830
do type coercion to string.
220
00:10:33,830 --> 00:10:35,573
So let me show you something else.
221
00:10:37,110 --> 00:10:42,110
So if we do 23, the string minus 10 the string
222
00:10:44,010 --> 00:10:47,823
minus three, what do you think will happen now?
223
00:10:48,920 --> 00:10:50,830
So let's actually check
224
00:10:52,390 --> 00:10:53,863
and it gives us 10.
225
00:10:54,780 --> 00:10:56,210
So what happened here?
226
00:10:56,210 --> 00:10:58,407
It looks like this time JavaScript converted
227
00:10:58,407 --> 00:11:01,240
the strings to numbers.
228
00:11:01,240 --> 00:11:03,750
And indeed, that's why we get 10
229
00:11:03,750 --> 00:11:06,750
because 23 minus 10 is 13
230
00:11:06,750 --> 00:11:08,760
minus 3 is 10.
231
00:11:08,760 --> 00:11:11,640
And so what this means is that the minus operator
232
00:11:11,640 --> 00:11:14,280
actually triggers the opposite conversion.
233
00:11:14,280 --> 00:11:17,630
So in this case, strings are converted to numbers
234
00:11:17,630 --> 00:11:19,710
and not the other way around.
235
00:11:19,710 --> 00:11:23,120
So instead if we use the plus,
236
00:11:23,120 --> 00:11:24,820
what do you think is gonna happen?
237
00:11:26,500 --> 00:11:29,040
Then the three is converted to a string
238
00:11:29,040 --> 00:11:31,413
and then the three strings are concatenated.
239
00:11:32,570 --> 00:11:33,403
Okay?
240
00:11:33,403 --> 00:11:35,380
So this is a very important distinction
241
00:11:35,380 --> 00:11:37,020
to keep in mind
242
00:11:37,020 --> 00:11:40,470
because this actually confuses many JavaScript beginners
243
00:11:40,470 --> 00:11:42,070
when they don't know about this.
244
00:11:43,930 --> 00:11:45,880
So let's try another one here
245
00:11:45,880 --> 00:11:49,190
and I'm again using 23 the string
246
00:11:49,190 --> 00:11:51,800
times two, the string.
247
00:11:51,800 --> 00:11:53,630
And again, you will see that these values
248
00:11:53,630 --> 00:11:57,303
are gonna be converted to numbers before.
249
00:11:58,190 --> 00:12:00,780
And indeed, that's why we get 46
250
00:12:00,780 --> 00:12:04,030
because both of them are now converted to numbers
251
00:12:04,030 --> 00:12:05,360
because that's the only way
252
00:12:05,360 --> 00:12:08,920
that the multiplier operator can work.
253
00:12:08,920 --> 00:12:11,763
And the same, of course, is true for dividing.
254
00:12:14,260 --> 00:12:18,820
Okay, so I hope that the distinction between type conversion
255
00:12:18,820 --> 00:12:22,270
and type coercion is now pretty clear.
256
00:12:22,270 --> 00:12:25,350
And now just to make sure that you actually got it,
257
00:12:25,350 --> 00:12:28,690
let's play a game called guess the output.
258
00:12:28,690 --> 00:12:30,883
So that's a game I just made up.
259
00:12:31,830 --> 00:12:34,420
So I want you to guess what happens here.
260
00:12:34,420 --> 00:12:39,420
So I'll write one plus one like this.
261
00:12:40,590 --> 00:12:45,590
And then we say n equals n minus one again.
262
00:12:52,800 --> 00:12:55,800
So what do you think n will look like
263
00:12:55,800 --> 00:12:57,960
when we log it to the console?
264
00:12:57,960 --> 00:13:00,710
So essentially, we start with one the string,
265
00:13:00,710 --> 00:13:03,403
then we add one and then we subtract one.
266
00:13:05,060 --> 00:13:06,950
So take a moment to think
267
00:13:06,950 --> 00:13:08,310
what this should look like
268
00:13:08,310 --> 00:13:11,380
and then let's take a look at the solution.
269
00:13:11,380 --> 00:13:13,773
And it is 10.
270
00:13:14,690 --> 00:13:16,940
So that's a bit counterintuitive
271
00:13:16,940 --> 00:13:18,430
but according to the rules
272
00:13:18,430 --> 00:13:19,840
that we just learned before,
273
00:13:19,840 --> 00:13:21,460
it actually makes sense.
274
00:13:21,460 --> 00:13:23,170
So here in the first line,
275
00:13:23,170 --> 00:13:26,140
one plus one will actually turn out
276
00:13:26,140 --> 00:13:27,930
to be 11, the string
277
00:13:27,930 --> 00:13:29,800
because we have one string here
278
00:13:29,800 --> 00:13:31,550
and then the plus operator
279
00:13:31,550 --> 00:13:33,160
will automatically convert the number
280
00:13:33,160 --> 00:13:34,120
to a string.
281
00:13:34,120 --> 00:13:38,850
And so the result of this one is the string 11.
282
00:13:38,850 --> 00:13:41,470
But then here we have the minus operator.
283
00:13:41,470 --> 00:13:43,110
And in the minus operator,
284
00:13:43,110 --> 00:13:45,970
whenever we have a string or more strings,
285
00:13:45,970 --> 00:13:48,220
it will then convert it to a number.
286
00:13:48,220 --> 00:13:51,140
And so here the string 11 will be converted
287
00:13:51,140 --> 00:13:54,880
to 11 the number and then 11 minus one
288
00:13:54,880 --> 00:13:56,143
is, of course, 10.
289
00:13:57,790 --> 00:14:00,010
Okay, let's do one or two more
290
00:14:00,010 --> 00:14:02,130
and let's do it in the console here.
291
00:14:02,130 --> 00:14:04,230
And we can actually clear the console
292
00:14:04,230 --> 00:14:06,650
of all this clutter here
293
00:14:06,650 --> 00:14:08,523
by clicking on clear console.
294
00:14:09,828 --> 00:14:14,828
So let's do two plus three plus four plus five, the string.
295
00:14:16,570 --> 00:14:18,570
And now you actually can't really guess
296
00:14:18,570 --> 00:14:21,404
because we can already see the solution there.
297
00:14:21,404 --> 00:14:24,640
So let's try to understand what happened here.
298
00:14:24,640 --> 00:14:27,950
So we start here with two plus three,
299
00:14:27,950 --> 00:14:29,380
which makes five.
300
00:14:29,380 --> 00:14:32,650
Then five plus four makes nine
301
00:14:32,650 --> 00:14:36,460
and then we end up with nine plus five, the string.
302
00:14:36,460 --> 00:14:38,270
And then as you already know,
303
00:14:38,270 --> 00:14:39,500
since we have a string,
304
00:14:39,500 --> 00:14:42,650
the plus operator will convert the other operand,
305
00:14:42,650 --> 00:14:44,800
which is nine also to a string
306
00:14:44,800 --> 00:14:47,973
and then we end up with 95 as a string.
307
00:14:48,910 --> 00:14:50,440
Okay, and another one.
308
00:14:50,440 --> 00:14:53,350
10 minus 4
309
00:14:54,950 --> 00:14:57,530
minus three,
310
00:14:57,530 --> 00:14:59,790
and keep in mind that these are strings,
311
00:14:59,790 --> 00:15:03,510
and then minus the real number two.
312
00:15:03,510 --> 00:15:05,343
But then plus five the string.
313
00:15:06,310 --> 00:15:08,330
So again, this looks very weird
314
00:15:08,330 --> 00:15:11,270
but the logic is kind of the same as before.
315
00:15:11,270 --> 00:15:15,040
So we have two values that are subtracted
316
00:15:15,040 --> 00:15:18,400
and so JavaScript will convert them both to numbers
317
00:15:18,400 --> 00:15:21,290
and so the result of this is six.
318
00:15:21,290 --> 00:15:25,140
Then six minus three is three.
319
00:15:25,140 --> 00:15:27,560
Then three minus two is one.
320
00:15:27,560 --> 00:15:30,640
And then we end up with the same situation as before.
321
00:15:30,640 --> 00:15:33,750
So this one will be converted to a string
322
00:15:33,750 --> 00:15:35,533
and then we end up with the string 15.
323
00:15:37,990 --> 00:15:39,000
Okay.
324
00:15:39,000 --> 00:15:42,680
And with that, I think this should be pretty clear now.
325
00:15:42,680 --> 00:15:45,160
And you might be wondering why we're talking so much
326
00:15:45,160 --> 00:15:47,400
about this but it's really important
327
00:15:47,400 --> 00:15:49,900
that you know about this right from the start
328
00:15:49,900 --> 00:15:51,640
so that you can write your code
329
00:15:51,640 --> 00:15:53,690
with all of this in mind.
330
00:15:53,690 --> 00:15:56,630
Now, many people actually don't like type coercion
331
00:15:56,630 --> 00:15:58,630
and think that it's a bad practice
332
00:15:58,630 --> 00:16:01,120
to rely on type coercion.
333
00:16:01,120 --> 00:16:02,180
One reason for that
334
00:16:02,180 --> 00:16:04,820
is that type coercion can, in fact,
335
00:16:04,820 --> 00:16:08,770
introduce many unexpected bugs into our program.
336
00:16:08,770 --> 00:16:12,100
However, this only happens when we don't really know
337
00:16:12,100 --> 00:16:13,260
what we're doing.
338
00:16:13,260 --> 00:16:15,130
So when we don't know about the stuff
339
00:16:15,130 --> 00:16:16,400
that I just showed you
340
00:16:16,400 --> 00:16:18,510
because if you know, then it's way easier
341
00:16:18,510 --> 00:16:20,590
to avoid these errors.
342
00:16:20,590 --> 00:16:24,800
So in my opinion, coercion is actually a great mechanism
343
00:16:24,800 --> 00:16:27,670
that is gonna allow us to write a lot less code
344
00:16:27,670 --> 00:16:30,900
and also to write more readable code.
345
00:16:30,900 --> 00:16:32,960
So really make sure to take some time
346
00:16:32,960 --> 00:16:35,480
to understand how type coercion works
347
00:16:35,480 --> 00:16:38,083
and then just embrace it in your code.
27055
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.