Deep JavaScript Foundations, v3

Primitive Types

Kyle Simpson

Kyle Simpson

You Don't Know JS
Deep JavaScript Foundations, v3

Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "Primitive Types" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle digs into JavaScript's type system and debunks the myth that everything in JavaScript is an object.

Preview
Close

Transcript from the "Primitive Types" Lesson

[00:00:00]
>> Kyle Simpson: Our first core unit is the types system and specifically, how we deal with types which we're gonna get to, it's called coercion. It's that evil thing that you've been told to avoid and you're gonna find out from me, that I think it's a critical missing component in your applications that you don't understand, and also utilize this within the code.

[00:00:19]
So, to understand it we actually have to work our way from the beginning. We have to work our way from the ground up. So we're gonna jump in and talk about the primitive types. Many of you have probably heard this assertion before. In JavaScript everything is an object.

[00:00:34]
>> Kyle Simpson: There is a reason for this statement, but this statement is a misconception, this is false. And that's a joke, by the way. [LAUGH] because false is not an object. False is an example of one of those values that is not an object. The reason behind why people say everything is an object, is because most of the values in JavaScript can behave as objects.

[00:00:58]
But that does not make them objects. We'll come back to that when we talk about boxing a bit later. So this is not a correct statement, this is not an accurate statement but it's one of the most pervasive statements. I did search and found thousands of examples of people saying things like this.

[00:01:12]
And saying them authoritatively, they've wrote books, and they wrote blog post, and they put their name on it. And they said this is a fact, and yet that's not what the specs says.
>> Kyle Simpson: Sometimes, people argue with me that it doesn't matter what the specs says. All that matters is, how it behaves in my code.

[00:01:33]
>> Kyle Simpson: To me that's a lot like telling a scientist, it doesn't matter why the apple fell from the tree. We saw it falling, it doesn't matter why. Yes it does, it absolutely does, it absolutely matters why. So, if we look at JavaScript, in this specification right near the very top actually, there's not much before this in the specification, it tells us straight up that there are primitive types.

[00:01:58]
Okay, and you'll see them listed here undefined, null, boolean, strings, symbol, number, and object. It list those out as we referred to as primitive types. So let's dive in to the primitive types, to understand what they are and how they differ or how they are distinct from, just simply referring to everything as an object.

[00:02:20]
>> Kyle Simpson: Number one, there is an undefined type. And as we'll see, the undefined type has one and only one value in it, and it is the undefined value. There's also string type. These are primitive strings, not the string object that you create in something like Java. This is just the double quotes or the single quotes string literal, right?

[00:02:39]
This is a primitive string. There's a type called string, there's a type called number, refers to all of JavaScript's numbers, although there's a little caveat there we'll come back to in a second. There's a type called boolean, that refers to the specific two values, true and false. In some languages, in historical days there was only zeroes and ones to reference the idea of true and false.

[00:03:00]
JavaScript actually has values called true and false. And they are a special type. And they behave differently, than this other types. And then, there's the object type. And yes, the object is a type. It has many subtypes to it. And I think that's where a lot of people get confused, is that just because they're subtypes.

[00:03:18]
Or just because there something called boxing, where something can behave as an object, that doesn't make it an object. There are things that are objects. So this are some primitive types, that exist in the JavaScript language. When you say X equals the number 42, or you say X equals quotes 42, you are saying as a developer, there is a different intent that I have for the behavior I expect out of this value.

[00:03:46]
That's what we mean by a type. The set of intrinsic characteristics, that we expect to be able to do with that value. So if we expect something different from numbers we can an arithmetic, then what we can do with strings like accessing characters, those are different values I mean different values types, okay?

[00:04:04]
There is also add it recently a few years back in years six, a lesser none type but the primitives of types on the less call simple, symbols don't get use to often a lot of white for JavaScript that use basically you create suto private keys on objects. Not really private, but kinda obscure.

[00:04:23]
So you don't see those used a lot, more probably used in frameworks than you're gonna see in your general JavaScript code, but it is a primitive type nonetheless. And then we have some other types, or other things that may behave like types that we should ask about. For example, what happens or how are we dealing with things that have never been declared?

[00:04:41]
>> Kyle Simpson: Is that a type? Sorta but not really, the spec doesn't really define it as a type. But it has a behavior to it, that we can expect and understand, and so if it has a behavior we could sorta at least with air quotes say it's a type.

[00:04:55]
>> Kyle Simpson: What about null, I didn't put that in the list and JavaScript calls that a type. But it's not in the list, so why is it over here? Well it's because, it's a little bit corcky.
>> Kyle Simpson: And this is just straight up, a historical bug and we'll see in just a moment.

[00:05:09]
>> Kyle Simpson: And what about functions though? JavaScript didn't even list out function. If you look closely in that previous list, it didn't list out function and yet we know intuitively, that values which are functions have a very specific behavior, that is different from values that are numbers for say for example, so isn't it a type.

[00:05:30]
JavaScript refers to functions essentially as a subtype of the object type. In fact, in certain places refers to them as callable objects. So, it's sorta a type but a subtype rather than a top level type. Arrays, we use this all the time, and it seems like they definitely have a specific behavior.

[00:05:49]
They're numerically indexed, and they have a length property that automatically updates. And they have various methods to them. So, are they a type? Again, the best way to think about them, the best mental model I've come up with is that it's a subtype of the object type. It's a special kinda object with this numeric indexing.

[00:06:06]
A special kinda object that has this magical length property gather thing on it, okay? So it's not really a top level type, but it's certainly a kinda value that has a certain kinda behavior, and it's useful for us to be able to distinguish. If we need to do something with an array, and we have this value we don't know if it's array, we need to determine is it an array?

[00:06:26]
>> Kyle Simpson: Speaking of determining, how are we gonna determine all of this things? And I'm gonna throw in, a little of forward thinking stuff. Because at the moment, this isn't actually in the spec. But it's like, stage three and probably gonna land in the spec, at least I hope soon.

[00:06:43]
And that's finally the JavaScript getting big and support, bigint support, large integer support. And when it does come to JavaScript, almost certainly when it lands in the language, it's gonna be another primitive type. So it's on this list only because, it's not actually here but it's actually already implemented for example in V8.

[00:07:02]
So in all of your Chrome based environments including node, you have big and support.
>> Kyle Simpson: And it's likely gonna land officially, within the language.
>> Kyle Simpson: There is obviously a caveat that until it's there, it's not there. And things have been, late stage three almost stage four and then been completely deleted.

[00:07:21]
So I can't guarantee, that there will be bigints but it's pretty certain at this point, I'd say at least 95%. So, this are some things that are values that we can expect certain characteristics from, and there's a critical detail that I don't want you to miss here, which is that most of this are not objects, right?

[00:07:42]
This are objects, all of this not objects. All of this may have or at least many of these may have object like behaviors that you can opt into, but they are definitely not objects. Unlike languages like C plus plus and Java, in JavaScript and in other dynamically typed languages, it is not the variables that have types.

[00:08:05]
It is the values themselves that have types. And that matches with that instinctual definition I just gave types that they assumed or instinctual characteristics we expect from the values. Well, of course. Let's refer to them as value types. That's a better way of distinguishing it than to try to get the types policed to jump all over us and tell us, no this don't qualify as types.

[00:08:25]
I've had that happen. I've had that argument many of time, okay? As far as I'm concern, if JavaScript calls it a type, I think it's a type.

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