Welcome back.
We're now on lesson two of Section three, single line and multiline commenting type coercion and variable
mutation.
First thing, we're going to talk about a single line and multiline comments and then we'll come back
to this and talk about coercion and variable mutation.
It's best practice to use comments frequently to document your code.
It's also extremely useful for commenting about code you're not sure is working properly or to test
various versions.
So let's take a look at our code.
So as you recall from our last lesson, we looked at this example of the global execution context.
So how could we document this further?
How could we do a better job explaining this here and cleaning this up?
OK, so let's make sure this is going to the right one.
So one thing we could do is add a single line comment and the way we do that is with two forward slashes.
And you could also do a controlled forward slash toggle between comments and not comments.
And we could explain this is the global execution context, so this is a form of documentation context
so we can explain to the reader of this code what we're trying to explain here.
And then we could jump in here and say.
Function, execution context.
OK, and we can go down here and instead of having these two lines, which they're really kind of sequential,
let's do a multiline comment.
So the way we do multiline comments is with a forward slash asterisk and then we can say eval execution
context.
And then when we have multiline comments, we don't actually need to have the original double line comments
anymore, we just need to end it with one of those.
And so these could be just on their own separate lines.
Multiline comments are nice, especially when you have a long paragraph that you want to document,
some people would like to do this with multiline comments just so that all the lines are indented to
the same degree.
Definitely up to you.
But the purpose here would be to.
Comment something out that is multiple lines, and especially for documentation purposes, if you have
a long paragraph that you'd like to comment out and break it down into multiple lines, you can certainly
do so with a multiline comment.
So that's a forward slash asterisk and end it with an asterisk forward slash.
So that's how you do it.
There's a very similar process that works in HTML as well with a multiline comment.
OK, so that is single line commenting and multiline commenting and what's nice about single line commenting
is it's easy to toggle those on and off to test individual lines of code.
But now let's get into the next section of this, which is type coercion.
So let's learn about type coercion.
Now, the purpose of coercion is just that JavaScript automatically converts numbers and booleans to
strings during concatenation with the plus operator and strings.
That's a lot of information.
Let's take that piece by piece.
OK, JavaScript is going to automatically turn numbers and true or false values into text during the
process of adding these pieces of information together in a line of text.
OK, so let's look at an example.
Now, the reason this is important to note is that we need to understand that certain pieces of data
are just going to be read and understood a little bit differently depending on the way that they're
used.
So let's start with.
Let and let's just set up these three variables, OK, and what's happening here is something called
declaration.
We're going to get into that in the next lesson a little bit more.
We just did a declaration of multiple variables.
Now we're going to assign them some values.
So let's just see.
This animal is a dog and let's do is hungry.
OK, make sure that you're using the same variable names that you've already set up, if you have a
typo, it's not going to understand it.
And of this new age equals 12.
OK, so as you can see, we have three variables here.
Animal is hungry and age.
And with this one noticed that the second word has an uppercase letter, this is called Camil Case,
and it's just a very nice way to be able to read a variable name that has multiple words in it.
So this is a standard across a lot of programming languages.
There are other ways to make variables easy to read if they have multiple words like snake case.
Which is like that.
Kabab case, which is like that, but the preferred way for variable names in JavaScript is camel camel
case like this.
OK, so we've got these three variables.
We've assigned values to them and notice that this one looks different than this one and looks different
than that one.
That's because this is a string and this is a boolean, which means it's a true or false value and this
one is a number.
So obviously, this looks like a line of text.
It's in quotes.
This looks like a number.
And this looks like it's not exactly the same as this.
It's not a line of text as understood by JavaScript.
It's not a string.
It's a special keyword that indicates that something is true.
So this type coersion right now that we're going to get into is to show you what happens when we automatically
push these together.
So let's console log.
My.
And then we use the plus to concatenate concatenation means we're going to glue multiple texts together
and then we don't need to put the variable in quotes, we just say the name of the variable and that's
going to execute or invoke that variable.
And then we do is.
So so far, what is that going to say that's going to say?
My dog is.
Let's add.
Age and then add years old.
Now, one thing I got to make sure of is that I have spaces in here because it's not going to add the
spaces automatically for me.
OK, so now we've got spaces and then let's add.
OK, so we went ahead and saved that and let's test it out.
My dog is 12 years old, so interesting just to note that even though 12 is a number, it changed,
it coerced, so it forced.
The number 12 to be understood as a different type, so the string is one type booleans and other type
numbers, another type, so we can actually force a Boolean to act like a string or force a number to
act like a string if we can cat need it in strings so we can actually cancel log.
We could do if.
If is hungry.
Then council log.
My.
Animal is.
Hungry?
So we have this conditional expression happening right here where we're saying, if my dog is hungry.
Then print out, my dog is hungry, is hungry, is true, OK, in this case it is true.
So it should print out to the console.
My dog is hungry.
And there you go.
It worked, my dog is 12 years old and my dog is hungry, so that's what we did to coerce the type of
12th and true to be acting as.
So let's just test to see what happens.
If we say our dog is hungry, we could say it's
true.
He's hungry.
OK, so now we're going to see what happens when we refresh.
So because it is hungry, we made this conditional expression in this if statement and said, if hungry
is true, then council log, it's plus true.
Plus, he's hungry.
So it turned out is true right here, which is a special word, it's not a string into a string because
we can cat needed with strings.
All right, so that is type coersion.
Variable mutation is the simple idea that you can take this and change the value.
Now, if I want my age to be 13, I could do that.
And that is the variable
mutation.
And if I change is hungry.
To false.
Now, if I try to run this again.
Do you expect it to run a second time?
Let's see.
Now, it only ran once.
In fact, if I come at this one out, just so you can see.
It doesn't run at all because now Hungary is false, so it does not meet the conditional expression
required to run this line of code.
So that is it for our review of single line comments, multiline comments, type coersion.
And variable mutation.
And we'll see you in the next video, we're going to talk a lot more about what variables are the different
versions of variables and how we can make those work.