# JavaScript: From First Steps to Professional Values & Data Types Exercise

Learning Paths:
Check out a free preview of the full JavaScript: From First Steps to Professional course:
The "Values & Data Types Exercise" Lesson is part of the full, JavaScript: From First Steps to Professional course featured in this preview video. Here's what you'd learn in this lesson:

## Anjana provides an exercise to practice determining the value type in JavaScript. A demonstration of using typeof to check value types is also covered in this segment.

Get Unlimited Access Now

Transcript from the "Values & Data Types Exercise" Lesson

[00:00:00]
>> Let us poke around with some values, shall we? You can use the typeof operator to figure out what these values are. And what I'm going to ask you'll to do, is before you type anything in, guess, what type of value of the ones that we've seen so far, are each of these values.

[00:00:25] So we're gonna take a moment to think about it to ourselves and then use our typeof friend to check our answers and then we'll talk through it together. Okay, so let's see. How do we feel about these values? Were we able to guess them? Sometimes, I would imagine at least one would have been very difficult to guess.

[00:00:51] Okay, so let's see. False, what type is it?
>> Boolean?
>> Boolean, exactly. It is the counterpart to true. We already discussed these are with no quotes around them. These are Boolean values, and they're the only ones. How about the second one in quotes, true? We already looked at it a minute ago, this is a, Stephen?

[00:01:11]
>> String?
>> A string, exactly. So we looked at this a second ago, I think I probably still have it in my history here, yeah. So typeof in quotes, true string. That's again different than if we do typeof just the word true. Okay, how about document.title? What did we get?

[00:01:34] It's also a?
>> String.
>> String, yes. So document.title, again, we said that it's gonna be, well, in this case it's new tab because I'm not even in a document. But if we typeof that, We expect some kind of title, some kind of text to describe this page or name this page.

[00:01:58] How about though, if we take a string like some string, and then we do .length. What did this come back with? What type of value does this come back with?
>> Number.
>> Number, so remember the .length we said, we ran into it earlier, it tells us on certain types of things.

[00:02:17] It tells us how many things are in this thing. So we use it earlier to find out how many children are in a certain element, or sort of the children of a certain element in our HTML document. We can also use it on strings, and we're gonna look at this in a second.

[00:02:32] So if I have some string and then .length, it's gonna give me a number and I can confirm that with typeof. And we'll talk in a moment about what that number is. How about the last one? Did anyone guess this? If you do, you have some kind of JavaScript psychic powers.

[00:02:54] This one is super weird. So we said that, okay, we talked about two types of nothingness in JavaScript. There's undefined, which if we typeof, It's type is called undefined. And then the other typeof nothingness is null, which really just represents nothing else. But, [LAUGH] if I right typeofnull, did you find object?

[00:03:22] And now, you might be saying Anjanette, you lied to us, when you said that null is a primitive value and that JavaScript distinguishes primitive values from objects. So why is null type object? Well, that is because sometimes JavaScript makes no sense. [LAUGH] So, as I mentioned earlier, JavaScript was implemented in like ten days in its original version.

[00:03:49] And there were certain historical accidents that we're still dealing with all of these years later. And one of them is the fact that typeofnull it says that it's an object even though null actually is a type. It is a primitive data type in JavaScript. So this is just a weird thing, and I bring this up not because you need to know that typeofnull returns object, that is really just an accident and there's interesting.

[00:04:15] You look it up on MDN and blog posts and stuff like that, there's interesting reasons and underlying low level like bit implementations of things that, why this is. But the important part is that sometimes JavaScript does stuff that we don't expect. But suffice it to say that despite all of its historical accidents and all the fallout from it being a language that was thrown together in ten days.

[00:04:40] Just to be a scripting language to interface with the document that has now gone on to basically conquer the world and exist in all kinds of other contexts and be one of the most powerful and most widely used languages out there. We still have some like gotchas, or as they're often called in the JavaScript community, wat, WAT.

[00:05:01] So you'll often hear about there's a weird JavaScript wat, where typeofnull is an object. That's something you might run into in your JavaScript journey. And I only bring this up now because sometimes stuff is weird. So we don't need to worry too much about this, this isn't gonna come up in our daily lives very often.

[00:05:21] But suffice it to say, that JavaScript is sometimes as these little Easter eggs that have interesting backstories but not a lot of logical explanation. So, any questions about typeof or these different primitive data types before we move on, and talk in more detail about one of them? Yeah, question from the chat.

[00:05:44]
>> Why is typeof undefined coming out as in quotes undefined?
>> Haha, so this is a great question. So for example, when I do typeof 42 I get number. Why do I when I do typeof a in quotes 42, I get string in quotes? So typeof, if we look it up, In our MDN friend, we can type in typeof and go to the page, this is the definition of the typeof, it's an operator.

[00:06:18] We're gonna talk about what operators are a little bit later. But the typeof operator essentially always returns a string. And the string corresponds to the name of the type that this thing is. So, types themselves are identified by these strings that indicate one of these several values. So string number, string, string, string, Boolean, etc.

[00:06:42] And that is how typeof works is it returns a string always. So great question. And as usual, if we have questions like this when we're continuing our JavaScript journey, our friend MDN usually knows the answer.