Transcript from the "Primitive Types" Lesson
>> 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: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: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.
>> 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: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?
>> 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.
>> Kyle Simpson: And this is just straight up, a historical bug and we'll see in just a moment.
[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?
>> 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: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: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.