[MUSIC]
>> Hi. My name is Aaron Powell,
I'm a Principal Cloud Advocate at Microsoft.
Today we are going to be looking at data types in JavaScript.
JavaScript is a weakly typed language.
That means that the type system is not as rich and
robust as you might find in other languages you've worked with.
If you're coming from a strongly typed language
like C-Sharp or Java,
then the type system is going to be a lot more
simplistic and a lot more flexible to what you can do.
In JavaScript, our variables are defined in a pseudo dynamic way,
which means that they type the AR is determined by assignment,
so we can reassign them.
But that does mean that we still have to
understand some of the types that we have available to us.
Things like numbers, strings, Booleans,
dates, functions, objects and
arrays are all available in the type system.
Now we only have one number type in JavaScript,
and that is actually a float value.
JavaScript doesn't differentiate between whether
something is an integer value or a float value.
It's just a number as far as JavaScript is concerned.
We also have some special types available to
us that represent different things in JavaScript.
Things like NaN, not a number,
to detect whether something that was tried to be
converted to a number is actually a valid number to start with.
Null and undefined also have special use cases that we
have within JavaScript and they mean different things.
Null and undefined are not the same in JavaScript.
Because the type system is loose,
it could be a little difficult to understand
exactly what you're working with from your variables.
So for that we have two operators to help us
out; typeof and instanceof.
Typeof will provide a variable tool,
so we do type of and variable
or value that we've received as a function argument,
and now we'll return us a string representation of
the underlying type in the JavaScript type system.
Now, this is not the same types
that we saw on the previous slides,
because if we were to do typeof array,
we would actually get object back.
Now that might be a little bit confusing,
but it's because an array is actually just a special object.
It has a few additional properties and
a few additional functions that a normal object doesn't have,
but it's actually just an object under the hoods.
We would then receive something like number or string with
lowercase starting letters that represent string and number types.
Instanceof, on the other hand,
is used to be able to determine if something was
created from a constructor of a particular data type.
If we had a variable and another variable,
instanceof and a constructor type number
with a capital N string with a capital S,
etc, then now we'll return as
a Boolean representation of where it was
created from that constructor type.
Now again, this can be a little bit
confusing because the constructor for
number is not what we'd use when we create a variable for number.
Let's say we did const one equals the literal one,
so just the one character on your keyboard and we
did one instanceof number with capital N,
then we'll return false because we
didn't create it using the number constructor.
It can sound a little bit confusing,
but there's different use cases for
the different types of operator testing,
whether it's typeof or instanceof,
to work out what the data type is.
You'll need to experiment to work out what is the way that you
need to be able to test in the applications you're building.
But as I mentioned before,
the type system in JavaScript is loosely typed or pseudo dynamic,
which means that we can actually change that at Runtime.
Let's say we define a variable, well this case,
we'll call it x and we assign it the string something.
On the next line down, we're assigning it the value one,
a literal number 1 of your keyboard.
Now, on the third line,
we're saying 1 plus hello and that is being reassigned x.
Now you might be wondering,
what's the typeof x and what's the value of x?
Because one is a number and one is a string,
how do we add those together?
Well, what JavaScript does,
is it will work out that,
well, you can't add those two numbers
together because they're not both numbers.
Instead, it will try and find a common denominator befallen,
and it will turn them both into strings.
We would end up with the result of 1hello as the value of x,
so it's now a string type.
That's because it is being able to coerce the value of
one into a string representation
of one and then can cut them together.
You can also get caught out with
equality because of the loose type system in JavaScript.
In the first example,
we're doing x equals zero,
double equals an empty string.
Now this will again,
not be an easy thing to be able to do in associative because well,
one is a number and one's a string.
But what's happened is that JavaScript is
going to coerce them to the same type.
Both these values are falsy values.
Zero is a falsy value in
a number and an empty string is a falsy string value,
so will coerce them into both being a Boolean type.
Then false equals false,
and that becomes true.
That's why x would be true in the first case.
It doesn't care about the original types,
it will just try and attempt to make them
the same types if you use a double equals operator.
Instead, we should probably use the triple equals operator.
This does a type safe comparison between two values.
It will no longer coerce them into being the same type,
so we will say, is the number
zero equal to an empty string? Well, no.
Because they're not the same underlying type and
immediately x will be assigned the value of false.
That's again something to be careful of
inside of JavaScript because of loose type system.
It's always recommended that you use
the triple equals operator to do type testing.