Loading
Current section: 3. Primitive Types 8 exercises
solution

Comparisons

Transcript

00:00 So let's get to it. First, I'm going to say, our, loose equals, so const loose equal, loose equal is assigned to a Boolean. So this we actually don't need that. That's the AI. It's just kinda helping us there.

00:17 But, here's our comparison. We'll talk about billions here in a second. But, here's our comparison. Price is double equal to the quantity. This is going to give us a warning that says this comparison appears to be unintentional because the types number and string have no overlap.

00:34 Well, let's see. What happens if I remove those types? Well, it still is gonna give me that error because like I said, TypeScript actually is pretty smart and it knows what those types are even without our declarations. But we'll get to that. We'll get to that here in a second.

00:46 It just bothers me to see those because I never I almost never do this. But, yeah. So TypeScript's pretty smart. It knows that we can't compare those two things. They could not possibly be equal to one another.

00:57 So this double equal is technically correct. Like, you can technically do this. And what's interesting about this is, what that actually evaluates to, which we'll look at here in a second. I'm gonna add this t s expect error comment right here. What this does is it says, hey, TypeScript.

01:14 There's an error on the next line. I know and that and it's fine by me. I'm gonna I know better than you, TypeScript. This error, I'm gonna let persist. So, that is kind of the point, of this exercise is to show you that, that error and and everything.

01:31 But I also want to show you what happens when you do a double equal comparison between a number and a string. So we're gonna leave it there for now and we'll see that here in a second. So then if we do a strict equality, now we're doing three equal signs. We're like, these are really, really equal and we'll see how that differs from the loose equal here once we see the output. So let's console log both of these and I'm gonna just we'll stop here and we'll save it.

01:56 Now this is very interesting. So loose equality, the double equals, that is true. So price double equals quality but price does not triple equal quality. So what's going on there? Well, in, in JavaScript world, early on, the double equal was I'm not entirely certain why it was done this way.

02:19 Somebody should ask Brandon at some point. I I probably have a long time ago. I just forgot. But, Brendan Ike is the one who created JavaScript. He's responsible for this funny situation.

02:31 So, when you're using double equal, it actually does not compare the types between those two things. So it's, it is going to coerce one of those values into the other value just to see if, like, these may not be the same type. They may not be number and number. But can I coerce one of them to be the same? This is basically never something you wanna do.

02:58 Almost almost never do you ever want to use double equals. It's just probably never what you wanna do. And so triple equals is really the way to go. Yeah. There are, like, some situations when we're talking about this value not equal to null that can also be undefined and stuff.

03:19 We'll talk about null and undefined later. But even in those situations, I wouldn't even do this. I have other ways to, to handle those situations. So, yeah, pretty much don't ever use double equal. It can be really confusing.

03:31 Always use triple equal. Alright. Great. So let's continue. We're going to create another variable, not equal loose.

03:40 So that is a pound or an exclamation point with an equal sign. That's basically the opposite of a double equal and saying it's not equal. And this is a not equal loose. We are not getting a type error here because these are both numbers. And it is perfectly reasonable to use not equals just, with with that, double equal version.

04:06 But, again, it's just better to use the triple equal version, to avoid weird situations where two variables are of different types. Luckily for us, we don't really have this problem as much anymore anyway because TypeScript helps us kind of avoid some of these issues. But, yeah, that's neither here nor there. Just do the triple equals version. Okay.

04:28 And then we've got our, greater than symbol, and we also have a less than or equal to. So it's less than or equal to. And don't, don't mix those up, by the way. It's not equal to or greater than. That is actually a function.

04:43 We'll look at that later. So, the greater than or less than sign appears first and then the equal to. So there's also a oops, const is greater or equal. This is what that would look like. So the greater sign first and then the equal sign.

05:00 And so that is helpful there and then we'll uncomment this and here we go. We've got our, loose equality, true. And then our strict equality is false. Then, these things are not equal. They're not equal.

05:13 They're, it's those values are ten and twenty. And then we've got b is greater than a. Yes. B is greater than a. B or a is less than or equal to b.

05:24 Yes. That is true. And, we could say, let's do 20, And now a is less than or equal to b. Yes. That's true.

05:31 And now this is false. So we'll look at booleans here in just a little bit too. But, that is comparisons. There's actually a bunch of other operators that you can, you can do here as well that we're not going to get into. They can get more advanced.

05:45 These are pretty much the most common ones that you're gonna be using, as far as operators go. So there you go. That is comparisons in TypeScript.