Transcript from the "Types and Q&A" Lesson
[00:00:00]
>> Brian Holt: It's really hard to teach type systems too. I teach TypeScript as part of my react course. And that's definitely the hardest section, teaching even intermediate to advanced JavaScript developers. When I was at LinkedIn, I had to put on like multiple day workshops, teaching people how to use TypeScript.
[00:00:17]
So it's not something that I like teaching up front. [LAUGH] There are programming languages, many programming languages that will enforce that these objects look exactly the same. It must have a teacher, it must have a course. And if it doesn't, the program simply will not run, right. It forces this sort of homogeny.
[00:00:41]
JavaScript doesn't care at all. It's like sure you wanna have multiple different types and all that kind of stuff. I can put true here and 14 and something else and it just doesn't care, doesn't care at all.
>> Brian Holt: And this needs a after elements list, must have broke something, that.
[00:01:08]
>> Brian Holt: As you can see here, it just doesn't care. This is a bad idea, right? [LAUGH] Well, now I'm gonna leave it like that. [LAUGH]
>> Speaker 2: So what if we wanted to add more attributes to the objects? Could we just slap on true at the end of each one and it'll show or-
[00:01:28]
>> Brian Holt: So if I wanted to add something to Kyle Simpson's course here, is that what you're asking me?
>> Speaker 2: Yeah.
>> Brian Holt: Okay, so you could totally do something like courses[0].isAGoodCourse. I'm gonna put true there or else Kyle will hurt me. [LAUGH] But now if you look down here you'll have isAGoodCourse, and that's been tacked onto that.
[00:01:53]
Does that address what you're talking about?
>> Speaker 2: Exactly.
>> Brian Holt: Cool.
>> Speaker 2: When you're printing it out in console.log, why are the names turned into strings?
>> Brian Holt: Because they are, so it's actually just convenience that this isn't actually wrapped as a string here.
>> Speaker 2: It's not treated as a special key or something?
[00:02:15]
>> Brian Holt: Nuh-uh, it's actually a string. So something kind of fun you can do here, if I want, I can actually console.log( Object.keys(courses[0])). So this actually gives you back an array of all the keys right. And again, there's no order, cuz this is an object, right? You can see I get back teacher and course as an array there as well.
[00:02:46]
But you can see they actually are strings.
>> Brian Holt: You can also ask type of something. So I can ask type of some thing. So that tells me that it's a string. 5, this will tell you that it's a number. But then things start getting a little complicated. If I asked for an object, it's going to give me an object.
[00:03:09]
But if I get asked for an array, it says, hey, that's also an object. If I asked for not a number, that's a number. [LAUGH] So NaN like this, if I try to do five divided by green, or something like that, it's like that's not a number. [LAUGH] So sometimes you will see that if he's trying to do mathematical operations on things that are not mathable.
[00:03:34]
That's a term I just made up. So be careful with type of. But occasionally, in my opinion, type of is useful for telling apart numbers and strings. And that's about it.