>> Hi, my name is Aaron.
I'm a Principal Cloud Advocate here at Microsoft.
Today we're going to be having a look at
a couple of demos of how we
can use the different data types with inside of JavaScript.
We're going to start off by creating a couple of variables.
We'll create one which is an array.
We call these people and we'll give it an array of people names.
Let's create some primitive types as well,
such as numbers and strings.
We'll do const one.
That's the literal. I will create a string.
We create a boolean.
We'll create a variable.
Sorry, we'll create an object variable, person.
This can have a firstName property and a lastName property.
Finally, we should create a function,
okay? A function sayHello.
It will take the person and we'll console.log "Hello".
We'll just do person.firstname.
We're not actually going to execute this function.
I'm just using it so that we can
have a look at the different data types.
Now let's just put out
some logging information and use
the different operators we came to test types.
We're going to start with the type of operator.
I just output, say typeof so that we know that this is
the type of operator and we'll do console.log type of people.
I'll just duplicate this line a few times and we'll do one string,
boolean, person and in one more and that say hello.
We'll come over to our console and we'll run that.
node .\demo.js.
We'll see you now have output of typeof we've received;
object, number, string, boolean, object, and function.
My slides look correct except for one, the first one.
The first one return typeof object
but we're provided with people which is an array.
Now this somebody, you've got to be careful with the type
of operator because type of
protest against the primitive type
with inside of the JavaScript type system.
Now an array in JavaScript is actually just an object with
a couple of extra methods and properties on
it that make it behave like an array.
Now, that can trip you up if you're doing
type of testing to look at what the variable is.
Everything else is correct but we're not so correct on the array.
Another way we can test is using the instance of operator.
Let's just output what the instance of operator
will provide us here instance of.
Then we'll do console.log.
This time we provide it with the variable name, so people,
instance of and then the constructor
we want to test it against array.
I've just got instance of incorrectly.
Now we'll duplicate this a few more lines
and update this with all of the other variables.
B, person, person, and say hello.
I just realized I put my name instead of array.
There we go. It's not an instance of me,
it's an instance of an array.
Then number, string, boolean,
object and lastly, function.
Okay, now let's execute the applet there.
Node demo.js and we'll see that we've received;
true, false, false, false, true, true.
We've got correct this time with our array.
It knows that the people is an instance of an array.
But number string and boolean for
our number string and boolean variables have not returned true.
They've returned false.
We got it correct again with object and function.
Now the reason for that is that
when we're doing an instance of test,
it compares against the instance
constructor that is used to create the variable.
In this case, whether it's an object constructor,
an array constructor, a number constructor, etc.
When we created our number, string, and boolean variables.
On lines one, sorry lines two, three,
and four, we used literal values not the constructors.
Instead, if I was to do a new number and provide it with a value.
This time, if we were to output,
let's just pop that up and then we'll do
node demo so it doesn't hide behind my head.
We'll see that this time we
have the correct output for its itself.
The second one is true because it is an instance of
the number constructor but if we have a look at the type
of test we received object because the type of it is
an object because it's created using
the constructor not using a literal.
This is where you've got to be careful around using
the different typeof an instanceof tests
against the variables and values you're being
provided to work out what data type they actually are.
It's always good to test a couple of different ways
depending on what the needs of that bit of code.
Thanks for joining me. I hope to see you next time.