Check out a free preview of the full Getting Started with JavaScript, v2 course

The "Types" Lesson is part of the full, Getting Started with JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle demonstrates how the typeof unary operator can be used to reveal the type of a value, including number, string, boolean, undefined, and object.

Preview
Close

Transcript from the "Types" Lesson

[00:00:00]
>> Kyle Simpson: Now, I've been referring to kinds of values, but we need to get a little bit more specific about that. Because there's a term in programming language speak for the kind of a value, and it's referred to as its type. A value that is a kind of number, that's gonna have a type called number, and one that's a string called string.

[00:00:23]
So the typeof that you see here placed in front of the number 42, or the typeof, line 3, placed in front of that string, "Kyle", that's actually also an operator. typeof is a unary operator where we give it a thing and it tells us what kind of thing we're dealing with.

[00:00:42]
And this isn't that important when you're dealing with, you wouldn't really do typeof 42, because you already know it's a number. But again, when we talk about variables in a moment, you'll see that we may not know what's in some variable. And a typeof is really important to help us know, I have a number here so I can do math with it, or I have a string in this one and I don't wanna do math, I wanna do sort of printing messages to a console or something like that.

[00:01:06]
So you'll notice that each of these values we've talked about have a different kind of return result from this type of operator. And down at the bottom you'll notice that we have kind of some special cases where remember null was one of those empty values similar to undefined, but here, the type of operator, line 7 versus line 12, it returns us different results.

[00:01:29]
For undefined, it gives us that string undefined, but for the null, it perplexingly gives us the string object. And really, that's just a bug from 20 plus years ago in JavaScript. Unfortunately the way the web works, we can't really always fix bugs in a language, because then we might break other stuff.

[00:01:49]
So that's just something that we've had to work around for the last 23 something years now. But notice line 13, we said that's an array, but when I do a typeof, I don't get a specific kind of result that tells me it's an array, it just says it's an object.

[00:02:04]
That's not a bug, that's just a less specific kind of an answer than we might prefer to get, we might have liked for it to say array. But what we're getting out there is that there are values that can be sort of subtypes, an array which has a very specific behavior.

[00:02:20]
Remember the numeric positioning, but it's a subtype of the object type. So that's why it's returning to us an object, and it turns out there are other operations that we can do to figure out more specifically. Okay, well, you tell me it's an object, but is it a regular object or is it a special array object.

[00:02:40]
We can determine those things, we can distinguish those things using other kinds of operations. This is something you're gonna need to do in almost every programming language that you'll ever write in, is you're gonna have values. And again, values have different things that we do with them. We do math with something like 3.14.

[00:02:56]
And we do printing to a string, to the console or a file if we have something like string Kyle. Those are all distinguishing things that we have to do in any program language. And they're all basically under this umbrella of determining the type of the value we're dealing with.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now