Hello again, France.
So in this video, we're going to learn how to mutate variables and also the how and why of JavaScript
and how it does type coercion.
Now, before we talk about any of that, I do want to show you one last variable type that I didn't
show you earlier.
So to show that I am going to say VAR and this one, I'm going to be typing job.
OK, so now the difference here is that I'm not going to assign anything to this variable.
So here I can declare a variable, but not assign a value to it.
So I wanted to see what happens when we now take a look at this variable.
So I'll just write console, not log and job.
Now, can you guess what's going to happen here?
I'm sure you can.
So why don't we go to our browser see here that yes, it is undefined coming from script, not J Asset
Line 13.
So this line here, Prince and undefined to the console.
So remember that one I showed you that slide, I mentioned the undefined means nonexistent.
And that's the case right here.
All right, so we declared the variable, but we didn't assign any value to it, and so therefore job
is simply undefined.
And that's what we see in the console.
So we can then later go back and assign it to something.
So let's say job is student, and what you see here is that I declared variable here, but then I assigned
a variable to it here.
So if we just do the console.log this time again, so I'll just write console, dot, log and job.
And then when we go to browser, we'll see student.
And there you go.
So from line 13, we still see undefined because when we launch this variable to the console, we haven't
declared a value to it yet.
But.
From Line 16.
This is where the student comes in after we have declared it in this line above.
So basically, we have strings here, we have numbers, we have a Boolean and we also have the undefined
right here.
So these four are the most important data types that I want to show you.
All right.
So now we'll go ahead and move on in this lecture will learn the variable mutation as well as the coercion
type.
So we'll continue with our example of Alex here, and we'll learn all about the variable mutation and
type coercion.
All right, so what I'm going to do is reuse some of these variables.
So name is still Alex.
And we've also defined the school, no as 20, we'll leave it there.
All right, so we have defined two different at a time string and no.
Let's see what happens if we want to print them together on the console and see what happens.
So we'll give it some space here and I'll write console log.
So let's basically do a string which contains Alex and tow.
OK, so inside the console, that long we can say name and then we can use the plus symbol, and we're
going to explain a little bit later that this is an operator.
But for now, let's just simply use this here to join the strings.
So then we can do a space and then another place to add the school, no.
OK, so we are hoping that this year will then become Alex Space Toe in the console.
But before we do that, I want to console.log this school number here as well.
All right, so here, I'll say console log school, no.
OK, so let's check it out.
I'll go to my browser.
And here we have Alex, 20.
OK, so how did this work?
Because remember that school number here is actually a number and not really a string, but it still
logs here as a string in this entire Alex tow string.
So this works thanks to something called type coercion.
OK.
And what that basically means is it JavaScript automatically converts typed from one to another as needed.
Pretty convenient, huh?
Now, again, in this specific case, it has converted this number here to a string in order to be able
to join these three strings.
So Alex is a string, then this space here is also a string and then school number.
The number will automatically be converted by JavaScript to a string as well, so that it can then join
all of these strings into one bigger string.
So I don't know, am I the only one here that thinks that's pretty cool, that JavaScript will do this
work for us automatically through the process called type coercion?
Now, other programming languages are not like this, we would indeed have to convert our variable because
JavaScript is kind of easier to use.
It takes all that work away from us and won't do it automatically.
Now joining strings like this here, is this one of the many examples of type coercion that we're going
to encounter throughout this course?
So I just want you to know that type coercion here happens all the time in JavaScript.
All right.
So why don't we try out another example with?
Yeah, so let's join some strings and create some more variables.
So I'm going to show you something a little different, which is basically just to declare many variables
on the same line and then define them later on.
So here, I'll say the school name to say Alex's school name, and then we can also define a variable
called has course.
OK, so here before we define a variable here job and then later here we assign a value to it.
Right?
So it's time we declared both of these variables here, and they are still undefined.
All right, so that's what we had here, so I declared the variable job in this case, and it was then
undefined.
Now, only later in this line that I then assign a value to that variable.
And that's exactly the same thing that we're doing here.
So we're going to declare multiple variables.
On the same line, and then I assign values to them here on these different lines.
All righty, then.
It's actually a pretty common practice in case we want to declare many variables all at once.
It's a bit cleaner to do it like this.
And all in one line and then simply assign the values a bit later.
So here, what I want to do is give some value to these variables.
So I'm going to say school name equals and I'll say Oak Academy and has of course, equals true.
Now what happens if we want to log to the console, a variable that has a Boolean in here, it's included.
So let's just try it out with console dot log.
All right.
Console that log in inside of that, I'll say school name.
And Plus has 35 courses, plus does it have any course plus and then has course?
Now here we can ask a question, and let's see what happens when we try to join the has course variable
here into this string.
So let's go to the browser and refresh the page.
And here, Oak Academy has 35 courses.
Does it have any cause?
True.
And so I hope that you notice that even the Boolean here gets converted to a string, yeah.
So JavaScript also does type coercion with Boolean.
Now, remember that we have here in other Boolean variable, and that is is registered.
And here in the console, its color is going to be different because it's a Boolean, but it does get
converted anyway.
So now let's let's just delete it out here.
And let's see, it should say undefined.
OK, so you see how even the undefined is coerced into a string?
All right.
So basically it's converted automatically into a string.
So now let's take the House course again.
And today, I suppose.
So sure enough, that is basically what the tape coercion does.
Something else that I pointed out to you and that hopefully you learned, is how to declare multi variables
on one line.
And then later on.
In this line, we assigned values to these variables.
So now, if I take this school name, put it into quotes.
What do you think we're going to see in the console, can you predict?
So I'll just go to my browser and here I'll refresh my page, and now instead of Oak Academy, I just
see school name, right?
Because the school name is not a variable.
It's just a string.
You got it, all right, so.
That's pretty much all you need to know about tight coercion.
So we are going to yeah, let's just pause right here and we'll going to pick it up in the next video,
so I'll see you in the next section.