Because of that, JavaScript is a dynamically typed language, the type is associated with runtime values
and not named variables.
Therefore the programmer can write quicker because we do not have to specify types every time.
It's nice feature, but it may sometimes cause unexpected bugs and errors.
And it happens especially when we deal with type coersion.
Type corrosion means that a value from one type can be converted to another on the fly.
Let's see what it exactly means, create variable A.
Which equals to ten plus five.
And run it in council.
It's definitely 15 and type of the result value is a number.
You see the color of it is blue now put five within quotes.
And make it strong, so what do you expect to get?
We have no number 10 and on the other hand, we have five, which is I think, let's see, we have one
zero five in council.
So let me explain what has happened here.
JavaScript engine has found plus sign in the statement.
And you know, that plus sign belongs to Earth magical operators, but simultaneously it works for string
values.
It concatenate them, for example, we can concatenate multiple string values like this, create another
variable be.
Which equals to hello.
Done plus, dear.
Plus, student.
And run this variable in council.
So you can see hello to your students, which is a string value.
All right, let's go back to the first example in which JavaScript engine has to decide either convert
to string or five to number.
The people who has created this nice programming language decided that in this case, it should be converted
to a string value.
And this whole process is called type conversion.
JavaScript engine has course turned to a string.
But the key point is that we did not ask for doing that to the engine when JavaScript engine.
So this statement is automatically coerced number into three.
Okay, let's see what happens if we use other arithmetical operator, let's say multiplication and you
see that we have 50, if we look at this number, will find that it has blue color because it's actually
no.
So JavaScript engine has caused five into number because multiplication is an arithmetical operator
and it's used with numbers only.
All right, let's consider another case, right, verbal a equals two, five plus ten plus the.
Which is string and luggage in council.
You see that we have got to be OK.
Now, let's place B before 5:00, right, verbal sequels to be plus five, plus 10.
And Reinertsen Castle.
So we have now be five, 10.
We have got different results in both cases, and it seems to be right.
Let's explain what happened.
As you already know, JavaScript engine with the codes from left to right.
So in the first case, it has found numbers for the first operation and has added them as the numbers.
So the value became 15 and after that it has caused it to shrink.
And we have got 15 P as for the second case engine.
So the string at the beginning of the value and then it has caused five to eight Twigg.
So we got B five after the first operation and in the same way it has caused 10 to string.
Therefore we got the string value B five 10.
Makes sense.
All right, remember that all of these things happen on the right time, on the fly time, caution is
very odd process, so you should understand it.
Well, type caution also accused with comparison operators, which you are already familiar with.
Let's write in console five, triple EcoStar five.
It's true and there is nothing surprising here.
Now writes five triple equals to five and triple equals to five.
From human perspective, you may expect your true as well.
Let's see.
And we have false.
Let's figure out what has happened.
Five triple equals five is definitely true and true as boolean value.
So we have got to after first triple equals.
Therefore the next triple equals has compared the boolean value.
True.
And the number five and therefore it became false.
Makes sense.
OK, actually, this is what happened on the fly through triple the cost of five, generally, balloon
values can be caught into numbers.
I mean, that true can be coerced into one and falls into zero to prove that.
That's right.
True double equals to one.
Double equal Equilar Compass, as we remember, only values and not data types, so here we have two.
The same happens with false.
If we write false double equals zero, then we will get through.
But in case of triple equals, we will have opposite values, so as I already recommended, in order
to avoid unexpected results which may cause errors in bugs that then are hard to find and debug, the
solution is to use triple EQUASS operator because it's always a better practice.
OK, if you are new in programming, you may be a little bit confused and you may think where you can
use those little details.
But trust me, when you work with tons of lines of code in the future, it will definitely help you.
All right.
And just like we have discussed what type corrosion is, how it works with comparison operators, and
we have seen some interesting and weird things.
OK, let's move on.