Transcript from the "Primitive Types" Lesson
[00:01:00] Number 2, we wanna look at the scope and the closure system. And number 3, the this and the prototypes system. And actually, that's more about class oriented even than prototypes. So those are the three main areas that we will look at through the rest of this course. Let's dive into types and coercion.
[00:01:17] The three things that we wanna look at underneath types and coercion are the primitive types. And if you remember from the primer at the top, we saw primitive types like number and boolean and string. We wanna get real clear on what those are. We wanna talk about how you convert from one type to another, because basically all programs have to do some kind of conversion from one type to another.
[00:02:02] And I think it might be one of the more important of the foundational pillars for this language. So it’s really important that you push against that normal temptation. Someone might tell you you don’t need to know about that stuff, just do it this way. I think it’s important to push against that and really dig in here and understand, these are core mechanisms to the language.
[00:03:45] So every once in a while, you might run across a symbol. But what also about null? You remember undefined was an empty type, what about null? Well, that is a primitive type, but I have that highlighted in orange because it behaves a little bit strangely with respect to the type of operator.
[00:04:00] It gives us not back what we would expect, so it's an important type, but it has some quirks that we have to be aware of. And then also function, we know functions are a thing in our language but, is it really a primitive type? Technically it's not, it's actually technically just a subtype of the object type which is the same is true for arrays.
[00:04:20] Arrays are not their own official primitive type. They are a subtype of the object type. So let's just point out that the types that we're talking about are associated with the values themselves, not with the variables. So in some programming languages, like for example Java or C, you say variable x is gonna hold integers.
[00:05:07] So it's not the variables that have types, it's the values. So, when I say var v, you might recall when we declare a variable that it initially gets assigned the undefined value and the undefined value is going to have type of, quote undefined. If I assign it a string one, and then I say type of v, I'm gonna get quote string.
[00:05:28] And then if i assign it the number 2, and i say type of, it's quote number. And so on for boolean, we see the object giving us quote object,. There is one of those ES 6 symbols, giving us quote symbol. I wanna highlight the undefined for just a moment, because this one's a little bit bizarre.
[00:05:45] It's been declared and it's been given undefined but there's another case where we can get the same return result of quote undefined and that is when the variable has never been declared. You might think that should give a different result, I certainly do, but it turns out that in either the case where it's been declared and has no value or it’s never been declared.
[00:06:21] We haven't created it with a var statement and yet we're still getting quote undefined. So again, be aware of the difference between a variable thats been created and has no value like v in the previous slide but also variables that you reference. And you don't often reference variables that are straight up, never exist, but you may be mistyped a variable name.
[00:06:43] And that will be in that state where the type of operator would give us quote undefined. I mentioned just a moment ago that null kind of behaves a little bit strange. We see here that type of v, when the value is null, we see that it gives us quote, object instead of what we might expect of quote null.
[00:07:19] Type of v there, we get quote function and even though function is not an actual primitive type, it's a subtype of object as I said. It does have its own unique return value here which might be a little bit surprising but it's actually very useful because we need to know if we have a value that can be called as a function.
[00:07:38] And then we see line 9, we create an array, and typeof is gonna give us quote object here, because it can't further distinguish that as a subtype of the object there. You might think, why did it do it for functions and not for arrays? And the answer, as with most things, is because reasons, because history, there's a variety of different reasons.