>> Hi, I'm Jasmine Greenaway.
In then this demo, we're going to do
some conversions with strings and numbers.
Throughout this demo, we are going to be using
two variables, num 1,
which is the number 150 as a string and flo 1,
which is a floating point number and it's
1.50 and it also a string.
In this example where we're converting strings to integers,
we're using the parseInt function.
This is built in to JavaScript
so you don't have to add a library or anything like that.
You just use it in the casing that you see here.
Lowercase p, uppercase I.
Here we're converting the string number 100.
Then we convert num 1 to the number of representation of 150.
Notice that we tried to convert the string ABC,
but it's not a number so
JavaScript prints "Not A Number" to the console as well.
Line 9 is a special case
because 0Xf does have non-numerical characters in it,
but JavaScript treats it like a hexadecimal number,
so it prints the number 15.
That is the integer form of this hexadecimal number here.
On lines 13-15 we're converting some strings to floats.
Notice that the casing is very similar parseInt.
In this first example,
we are converting the number of
1.00 to a floating point number and it just prints one.
It drops the zeros after this.
There are ways to format this if you would
prefer to preserve the zeros after this one.
In line 14, we are using the variable 1.5,
that was a string to convert it to the floating point number 1.5.
Again on line 15, we are trying to convert a string
that is not numerical to a number and it prints "Not A Number."
On line 20 and 21 we're just doing some more examples here.
Notice that parseInt is trying to convert 1.5.
What happens here is that parseInt will ignore
any characters and then ignore
any numbers that have to be after any special characters.
The only characters are allowed in parseInts are numbers or
numbers that represent
either hexadecimal or octal or things like that.
Here it just prints one.
Now say this was 0.5,
it would actually print not a number because the first thing it
reads is that period character and that's not a number.
On line 21 we're doing a similar example.
Here we have one plus one,
and you would expect that maybe you would print two.
But remember, this one plus one here is a string.
What it's going to do is going to look
for any numbers as the first character.
What's happening here is that parseInt
sees the one character here and it converts that to one,
notices that there's a space,
then notices that there's a plus sign after here,
which can't be parsed to numbers.
It just ignores everything after
the plus sign and actually everything after the space,
and then just prints the number 1.
The line 24 is a pretty special case here,
where it uses template literals.
A template literal is
a string and the cool thing about it is that you can
evaluate expressions inside a template
literal before you do anything else with it.
Here we are evaluating the expression one plus one.
With these placeholder characters here,
the dollar sign and the curly braces.
Then we evaluate that to two and then we convert it with parseInt.
At this point it is
a string representation of the number 2 and then
parseInt goes and finishes
the work of converting it to two and that's why it works here.
In this final example,
we're converting some numbers to string.
This is pretty straight forward,
no matter what number it is,
you're just going to use the method
toString to convert it to a string.
Notice that again, it is case sensitive and also
keep in mind to add the open and closed parentheses here as well.