We have briefly in the earlier lessons, looked at some data types, and we're going to look at this
in more detail in this lesson, that script standard, there's several different standard data types.
So there are booleans.
So we know those are either true or false.
There's numbers.
So those can be written with or without a decimal place.
There's strings so they can be written with a single or double quotes.
There's also NUL.
So this is explicitly nulls.
So there's nothing.
And by default, when you declare a variable and if you haven't assign any value to it, then it's comes
back as undefined and there's also symbol and symbol value.
So it represents a unique identifier symbol.
It's new in ECMAScript 2015 and it's data type whose instances are unique and immutable and everything
else or almost everything else in JavaScript is an object type.
You can also, with JavaScript, convert between different types.
And we have seen this as well, where when we've got we can start out with a boolean or we can start
it with the string and then when we assign a number to it, the data type becomes a number.
So there is flexibility with JavaScript, but you can also force the different data types and the type
conversion so you can convert a number to a string by wrapping it with the string methods typing in
string and then the rounded brackets and then using the value or the variable.
That's a number.
And that can turn that into a string.
And there's also arrays that can be turned into strings and there's more on arrays coming in the upcoming
lessons.
You can also use the dot to string methods are changing the one value together with another one, and
that's going to turn it into a string so you can convert it into a number so you can force a string.
And as you can see, the 10 has the quotes around it.
So this is a number.
You can also turn a boolean into a number.
And if you try to turn something that is not a number, could never be a number into a number, you're
going to get this.
And again, so that stands for not a number.
And we can all log these out into the console and try these and take a closer look at this.
There's also type core coersion, which is converted by JavaScript.
If we add a number to a number or if we had a string or something, that's not a number to a number,
then if it can't be converted into a number, then it's going to automatically turn it into a string.
So JavaScript tries to do some of the thinking for us and trying to convert the different values into
the appropriate data type.
And more often than not, this is a common issue when you have values that you think are numbers and
you try to add them together and it turns out to do ten plus five and it ends up with something like
one or five instead of 15.
So we find that when we are doing data types, sometimes it's a good idea to force the value and make
sure that it is the data type that we are trying to work with.
You can also have some fun with the different data types.
So one of the JavaScript quirks is that the data types are dynamic.
So this means that JavaScript does try to convert it and we can of course, convert it with the number
or string methods which allow us to force various data types.
And as you can see in these examples.
So if we do hello plus world, we end up with a string hello world.
If we do five, which is a string plus a number string, we're going to end up with fifty five, ten
minus five.
We're not going to be able to subtract it, but it is going to convert this into a number and then we're
going to end up with five.
So there are a bunch of little strange things that do happen with JavaScript and you can try these out
in the console in order to get more familiar with it syntax to use in order to get data types.
So if we do, type of that will return the different data types.
I'll show you how to do that in the console and then also output that information into the console.
So if we have a five and we want to find out what data type it is, we want to output that value and
see what data type the five is.
So opening up our browser and we can see that we have a value that's a string.
So if we have a string and if we add a five to it, we end up with the 55 string.
If we have a 10 and we subtract a five to it, we end up with a five.
So that's one of the weird quirks of JavaScript, is that this one, although they both started out
as strings, when you subtract it, then it automatically thinks, OK, well, because we can't subtract
one string from another.
So then it automatically tries to convert this first one to a number and this is what happens.
So this is what I meant by there are some weird quirks that will happen with JavaScript.
So if we take something like hello and if we subtract world from it, we're going to see that.
And and so that's not a number because of course, we can't subtract words one from another.
So that's a little bit confusing.
And JavaScript tries to address it and adjust it.
And if it can't, then it simply returns back that end.
And you can also set up a variable.
So if we set up a variable and if we set up a variable of a as five.
Type of a you're going to see that it gets returned back a string type of and if we have a value that
is a number, you can see that it gets returned back as a number.
And also there's type of and this one is going to be a boolean and it's going to know it's a boolean
because booleans, again, are either true or false.
So we know that those are booleans.
If you do type of null, it gets return back as an object type of undefined and that just gets returned
back as undefined because undefined is its own data type.
It is good to become familiar with them.
And as we saw with a.
So we've got a is currently string value and you can convert it to a number, convert it into a number
so we can take the value of string and we can turn it into a number.
So when it's blue it's being returned back as a number, a value of one hundred.
So this is clearly a number and we can convert it into a string using the two string method string as
well.
So there are actually two ways to convert it into a string.
So those are the two examples of how we can convert it.
And then if you ever want to find out the type, all you have to do is type in.
Type of.
So let's go back to our challenge.
And the challenge is to create a string convert to different data types and then get the data type of
the variable and output that type to the console solution.
We're going to create a number, give it a string value of five, then take that string and convert
it into a number, outputted as a number, and then check the data type of the myname.
So make sure that it has converted.
So go ahead and try this out in your editor.
You can pause the video and I'll show you the solution.
Open up the editor and let's set up myname and this can be any number that we want.
So A set my number to one hundred and then we'll take and what we'll do first will use console log and
will output the type of my num so that you can see that it actually is being converted.
So when we refresh we see the type as a string and convert it.
So there is the number and we're taking the variable and we're converting it through.
It's being returned back as a number.
And if we log out the value of my NUM, you can see as well Chrom is outputting it as a number since
it's blue and wait, hold everything before we conclude, I have another challenge for you.
So this is a bonus challenge.
Did you get a chance to try some of that in your console where you're adding maybe two strings together,
maybe two numbers together and maybe a string and a number and see what happens?
So try that out as well and output the value of five to ten and then fix A plus B to equal 15.
And remember converting those two numbers in order to add them together.
So the solution for this one setting up a as five, setting up B as ten, and then we want to do is
add those together in the console, but we want to have a value of 50 instead.
Right now we're getting five, ten.
So let's fix that.
And we can convert these two numbers by wrapping that with the number method and again wrapping it with
the rounded brackets and having the value passed inside.
So that will force A and B to be numeric and in fact, allowing us to add them together and return back
15.
So try that one out as well.