Loading
Current section: 3. Primitive Types 8 exercises
lesson

Intro to Primitive Types

Loading lesson

Transcript

00:00 Alright. Welcome to the primitive types exercise. This is gonna be great. So JavaScript is, technically not a typed language as you might think if you've used other typed languages in the past, But it does have types, and each value in a variable has some, type. And, ultimately, they all lead down to these primitive types.

00:24 So here, we have age and name. This is gonna be a number and this is gonna be a string. And, they have certain properties that they, can have. They have functions that you can call on them. All of that is is great and interesting.

00:38 In TypeScript, you can actually be more explicit about the types that flow through your application, and it has a lot of niceties to it. So you can, give it an explicit type, as you're defining the variable, and this is the syntax for that. There are, types for each one of these things, and you can actually go way beyond this into non primitive types, that we'll talk about in the future. So, we're gonna be mostly working with these. Although, personally, I prefer inference, which is what happens here where TypeScript sees this and it's like, oh, okay.

01:12 I know age is gonna be a number. I know a name is going to be a string. I don't need you to tell me because I can just see from the assignment here. Sometimes it is useful to be explicit about the type that your variable is gonna be especially if you're using let. You can say, let, add, you know, address be a string, or it will be undefined, and then you can later assign it to be that string.

01:37 So you do definitely use this type of syntax here for specifying what the type is going to be. There are, like I said, seven different, primitive types that you can have in JavaScript. There are number, string, Boolean, null, undefined, big int, and symbol. And we're gonna talk all about them, and it's gonna be great. Types are really important in TypeScript programs because in traditional or or in JavaScript, that's the thing actually running in the browser.

02:09 Even though it does have types, in the variables, it doesn't enforce any of those things and instead will throw errors at the time that the code is running, whereas a traditionally typed language is going to throw errors at the time it's being compiled. So before the code even runs, it's going through the computer goes through all the text, turns that into its own representation of that text, and that step is called a compilation or compiling. And during that compilation, a typed language will say, oh, hold on. You're passing this function or this value to this function, and that function says it's not allowed to accept that type of value. So I'm going to throw an error, and so you don't even get to the part where you're running code.

02:50 Whereas with JavaScript, you can have those kinds of errors all over the place and it will run until it hits one of those and then you get what's called a run time error. So runtime errors are really hard because it's difficult for you to test for every single possible case as you're, writing your program. And so by having, TypeScript, a proper compiler that, can run ahead of time, you can, be notified of any of those problems before you ship your application to production. So that is what we're doing right here. We've got this function here, and we'll talk about functions later.

03:25 This is just a bit of a preview. But, we're saying it accepts x, which is a number, and it's gonna return a number. And, when you try to pass a a non number to it, then the compiler can yell at you and be like, oh, no. You're not allowed to do that. We also have the null and undefined types.

03:42 We'll talk about those. They're similar but different. We have truthy and falsy which is very useful weirdness. And we have big int and symbol, rarely used but also quite useful. And we're gonna talk about all of these things in the primitive types exercise.

03:56 I'm excited for you to do this, so let's get into it.