WEBVTT

00:01.940 --> 00:08.090
Because of that, JavaScript is a dynamically typed language, the type is associated with runtime values

00:08.330 --> 00:09.740
and not named variables.

00:10.100 --> 00:15.560
Therefore the programmer can write quicker because we do not have to specify types every time.

00:16.400 --> 00:20.540
It's nice feature, but it may sometimes cause unexpected bugs and errors.

00:21.230 --> 00:24.050
And it happens especially when we deal with type coersion.

00:25.460 --> 00:31.120
Type corrosion means that a value from one type can be converted to another on the fly.

00:32.520 --> 00:36.120
Let's see what it exactly means, create variable A.

00:37.630 --> 00:39.730
Which equals to ten plus five.

00:41.340 --> 00:42.690
And run it in council.

00:46.900 --> 00:51.740
It's definitely 15 and type of the result value is a number.

00:51.790 --> 00:56.770
You see the color of it is blue now put five within quotes.

00:57.730 --> 01:00.910
And make it strong, so what do you expect to get?

01:01.450 --> 01:10.210
We have no number 10 and on the other hand, we have five, which is I think, let's see, we have one

01:10.210 --> 01:11.770
zero five in council.

01:12.910 --> 01:14.860
So let me explain what has happened here.

01:14.950 --> 01:18.130
JavaScript engine has found plus sign in the statement.

01:18.670 --> 01:25.810
And you know, that plus sign belongs to Earth magical operators, but simultaneously it works for string

01:25.810 --> 01:26.290
values.

01:26.830 --> 01:34.240
It concatenate them, for example, we can concatenate multiple string values like this, create another

01:34.240 --> 01:35.560
variable be.

01:36.850 --> 01:38.440
Which equals to hello.

01:40.300 --> 01:41.740
Done plus, dear.

01:44.580 --> 01:45.810
Plus, student.

01:48.290 --> 01:50.630
And run this variable in council.

01:55.600 --> 01:59.800
So you can see hello to your students, which is a string value.

02:00.970 --> 02:06.970
All right, let's go back to the first example in which JavaScript engine has to decide either convert

02:07.570 --> 02:09.400
to string or five to number.

02:10.330 --> 02:17.230
The people who has created this nice programming language decided that in this case, it should be converted

02:17.230 --> 02:18.550
to a string value.

02:19.090 --> 02:22.210
And this whole process is called type conversion.

02:22.390 --> 02:25.270
JavaScript engine has course turned to a string.

02:25.840 --> 02:31.840
But the key point is that we did not ask for doing that to the engine when JavaScript engine.

02:31.880 --> 02:36.160
So this statement is automatically coerced number into three.

02:37.040 --> 02:44.950
Okay, let's see what happens if we use other arithmetical operator, let's say multiplication and you

02:44.950 --> 02:52.120
see that we have 50, if we look at this number, will find that it has blue color because it's actually

02:52.120 --> 02:52.510
no.

02:53.140 --> 03:00.370
So JavaScript engine has caused five into number because multiplication is an arithmetical operator

03:00.370 --> 03:02.170
and it's used with numbers only.

03:03.680 --> 03:13.730
All right, let's consider another case, right, verbal a equals two, five plus ten plus the.

03:15.750 --> 03:19.140
Which is string and luggage in council.

03:22.130 --> 03:26.020
You see that we have got to be OK.

03:27.320 --> 03:35.870
Now, let's place B before 5:00, right, verbal sequels to be plus five, plus 10.

03:38.580 --> 03:40.470
And Reinertsen Castle.

03:44.620 --> 03:48.130
So we have now be five, 10.

03:49.230 --> 03:53.250
We have got different results in both cases, and it seems to be right.

03:53.970 --> 03:55.170
Let's explain what happened.

03:55.650 --> 03:59.830
As you already know, JavaScript engine with the codes from left to right.

04:00.270 --> 04:06.750
So in the first case, it has found numbers for the first operation and has added them as the numbers.

04:07.440 --> 04:12.090
So the value became 15 and after that it has caused it to shrink.

04:12.480 --> 04:17.610
And we have got 15 P as for the second case engine.

04:17.640 --> 04:23.000
So the string at the beginning of the value and then it has caused five to eight Twigg.

04:23.790 --> 04:30.690
So we got B five after the first operation and in the same way it has caused 10 to string.

04:31.230 --> 04:34.440
Therefore we got the string value B five 10.

04:36.220 --> 04:36.910
Makes sense.

04:38.440 --> 04:45.040
All right, remember that all of these things happen on the right time, on the fly time, caution is

04:45.040 --> 04:47.590
very odd process, so you should understand it.

04:47.590 --> 04:53.640
Well, type caution also accused with comparison operators, which you are already familiar with.

04:54.280 --> 04:58.690
Let's write in console five, triple EcoStar five.

04:59.410 --> 05:02.020
It's true and there is nothing surprising here.

05:02.830 --> 05:08.020
Now writes five triple equals to five and triple equals to five.

05:08.980 --> 05:12.540
From human perspective, you may expect your true as well.

05:13.420 --> 05:14.050
Let's see.

05:14.320 --> 05:15.600
And we have false.

05:16.630 --> 05:18.190
Let's figure out what has happened.

05:18.940 --> 05:23.890
Five triple equals five is definitely true and true as boolean value.

05:24.520 --> 05:27.310
So we have got to after first triple equals.

05:27.880 --> 05:31.680
Therefore the next triple equals has compared the boolean value.

05:31.690 --> 05:32.110
True.

05:32.500 --> 05:36.190
And the number five and therefore it became false.

05:37.690 --> 05:38.370
Makes sense.

05:40.270 --> 05:47.920
OK, actually, this is what happened on the fly through triple the cost of five, generally, balloon

05:47.920 --> 05:49.710
values can be caught into numbers.

05:49.720 --> 05:55.570
I mean, that true can be coerced into one and falls into zero to prove that.

05:56.560 --> 05:57.330
That's right.

05:57.340 --> 05:59.410
True double equals to one.

06:01.000 --> 06:07.450
Double equal Equilar Compass, as we remember, only values and not data types, so here we have two.

06:08.050 --> 06:09.790
The same happens with false.

06:10.270 --> 06:14.980
If we write false double equals zero, then we will get through.

06:17.140 --> 06:24.880
But in case of triple equals, we will have opposite values, so as I already recommended, in order

06:24.880 --> 06:32.230
to avoid unexpected results which may cause errors in bugs that then are hard to find and debug, the

06:32.230 --> 06:37.270
solution is to use triple EQUASS operator because it's always a better practice.

06:38.980 --> 06:44.650
OK, if you are new in programming, you may be a little bit confused and you may think where you can

06:44.650 --> 06:46.060
use those little details.

06:46.480 --> 06:52.020
But trust me, when you work with tons of lines of code in the future, it will definitely help you.

06:53.560 --> 06:54.040
All right.

06:54.670 --> 07:00.370
And just like we have discussed what type corrosion is, how it works with comparison operators, and

07:00.370 --> 07:02.530
we have seen some interesting and weird things.

07:03.460 --> 07:04.830
OK, let's move on.
