Loading
Current section: 3. Primitive Types 8 exercises
solution

Null And Undefined

Transcript

00:00 So this one's gonna be pretty quick. We're just going to make a variable discount code as undefined, and we'll have the last purchase date set to null. So undefined itself is a type, and you assign it to the value undefined. So undefined is actually a keyword both in JavaScript and in TypeScript. It is a type and it is the value for itself.

00:20 So it's kinda funny. It's undefined equals undefined. I promise we'll talk about how to get rid of this here in a little bit. And then for null, it's the exact same thing. It is a keyword representing the, a thing that doesn't have a value, and, it is also a type in, TypeScript speak.

00:39 So, let's go ahead and log both of these and export them, save this, and there you go. It's exactly what you'd expect. There's not a whole lot of interesting things going on here with this, but the, key thing to keep in mind here is that you can actually have a value or a variable declared that doesn't have any initialization. So here, this is giving us an error because, the discount code has to be assigned to something because it's const. But if we use let, it doesn't have to be assigned to something.

01:11 And now, discount code is still undefined. So it has not been defined. That's kind of the distinction that I make between null and undefined. It's like this value, we have not yet decided what it should be set to. Whereas, null is like, this value, we have decided what it should be set to and it should be set to nothing.

01:30 That's kinda how I think about it and, you'll find, values that you accept in your functions that might be null, might be undefined. You'll have, values that you need to pass to something, might be null, might be undefined. And the the distinction here is that this thing does exist, and it has been thought about the it's been thought about to be nothing, or to be null, versus, we didn't even provide this. We didn't even specify this. When we get into objects, this will make a lot more sense as well.

02:00 So I'll just give you a little preview. So if I say, const person equals, name John h yada yada yada. So, if I look at console dot log log person dot name oops. Name. Then here, we're gonna see our log of John right there.

02:21 If I say person dot, address, in this case, that's going to be undefined. And so the undefined is basically the lack of any initialization, is the way that I think about it. So here, lack of initialization, lack of initialization. There is no address for this person. There is no discount code set here.

02:42 But you can intentionally and directly assign it to undefined. It doesn't really change the behavior in this case, though it would let you actually, use a const declaration. So there's that. But, yeah, that's the distinction there. And we could we could say address is null, as in, this person really doesn't have an address.

03:02 It's not that we forgot. It's that they just don't have an address. So hopefully that makes a little sense, and and feel free to reference back at this exercise if you ever need to kind of review. Okay. What's the difference between all and notified again?

03:15 Because they are pretty similar, in concept. But you think about them a little bit differently in practice. Alright. I hope that was helpful. We'll see you in the next one.

03:25 Dot problem. Thanks.