Loading
Current section: 3. Primitive Types 8 exercises
solution

Truthy Falsy

Transcript

00:00 Alright. Let's go ahead and solve this one. So we've got a bunch of values right here. Let's find out what those values evaluate to. So we're gonna make a, user or a has username, variable here.

00:11 We're gonna pass that to the boolean constructor and give it our username. So this is less of coercion and more of like creating a Boolean value by using the Boolean function or constructor here. So it's a little bit less coercion, a little bit just like, hey, Boolean. Is this truthy or is it falsy? You can do the exact same thing, with a double not.

00:40 So what this does is it says, is the or it coerces username into a boolean. If it is truthy, then it will coerce into true. If it is falsy, it will coerce into false. And, and then we're going to not that. So if, when once it's been coerced, we're going to do the opposite.

01:00 If you remember when we talked about booleans, this not is gonna say the opposite of the next thing. So we coerce it into a boolean and then we do the opposite of it. So username being the string of Kent, I'll just spoil it for you. Kent is definitely truthy. Any any string that has contents is considered to be truthy, but then we're gonna not that, so now it's falsy.

01:23 So if our goal is to say has username, then we need to see we don't want to not that. We want to coerce it into a boolean. So what can you do? You can actually double not it. So, you coerce this into a boolean.

01:38 That'll be true. You have to do the not or or the the exclamation point to make that coercion happen and so that turns it into false. And so you do another not so that another exclamation point to, do the opposite of that. So coerce to a boolean. It's not or do the opposite and then do the opposite again.

01:58 And that is whenever you see that double, bang or the double exclamation point like that, that's what's going on is they're coercing it into a boolean and then, and getting what that boolean value is. It's kinda funny. I actually don't like doing this. I don't I used to. Early in my, JavaScript days, I do that all the time.

02:16 Now I pretty much almost always will do the boolean, approach instead. I think it's just a lot more clear what's going on there. But I if you and I were working on a team together and you submitted a pull request or or code change, that included the double not, I'm not gonna make a big deal out of that and that I don't care that much. That's that's where I'm at there. Okay.

02:38 Great. So that one was kinda fun. Now let's create a has nickname based on the truthiness of nickname. Now there are, we can just Booleanify all of these, values, and, that should make a little bit of sense. I think it'll actually be more interesting when we see the exports and outputs.

02:57 So let's just work through these really quick. And I want you to kind of guess in your head. Okay. Which one of these is gonna be truthy? Which is gonna be falsy?

03:06 Is, this string gonna be truthy? Is that empty string false? Is this zero going to be true or false? Etcetera, etcetera. Okay.

03:13 So now we can say can check out. This one is gonna be true when the cart total is truthy and accept, has accepted terms is true. Now this one's interesting because you can pass that in a Boolean and that will work just fine. But because we're using the logical operator here, we actually don't need that. By using the logical operator, we're kind of, side stepping that a little bit.

03:39 I'm gonna show you how that, changes here in a second and why you might not wanna do this. But we'll we'll take a look, at that, here. But you'll see that, here we get false and, spoiler alert, I guess. But we'll we'll take a look at this here again in a second. And then let's do a can create account.

04:00 This is gonna be truthy if all three three of these are truthy. And, you can pass all three of these. Even though this is an email and this is a password, you're gonna see what that does here in a second. Okay. So console log all of these things.

04:14 We got the exports. Let's save it and let's see. So first, has username. Alright. Boolean on the username, that's true.

04:22 So a string that has value, has contents, that is going to be truthy. So when you course that into a Boolean, we'll call that true. And that is really useful inside of, like, an if statement. If username, then, say hello username. Else, say hello unknown person, whatever.

04:40 So that's where this gets to be really useful. And then you have an empty string. So the nickname, that's false. That's false. You get an empty string.

04:48 We're not not having that. And then same with the age. If it's zero, that is considered a falsy. But let's skip ahead. The, cart total oh, we're not showing that in here.

05:00 Let's see. Cart total. Let's just console log Console dot log the cart total. Booleanify that guy. And right here, that's truthy.

05:11 And the other thing, if it's negative, that is also truthy. If it's negative zero, which is allowed, by the way, that is also false. Don't use negative zero. I don't think I've ever in my life used negative zero. So there we go.

05:30 We've got the empty strings. Those are gonna be falsy. Strings of contents, those are true. Of course, false is gonna be falsy. Undefined.

05:37 So notes, has notes. Let's look at that. Has notes is false. Undefined is gonna be falsy. Also, null is also falsy.

05:45 So where's our Hess notes? There it is. Falsy. So, that is an important thing to understand. Undefined, null, empty string, zero, these are all going to be falsy.

05:58 And it's actually really useful. You can say, I have this array of users. We'll talk about arrays later. But I've got this set of all these users, and it could have, like, a 100 users in it or it could have no users in it. And I don't really know.

06:13 So I wanna have conditional logic. If they have no users, I wanna display the message, you have no users. And so I can say, if users dot length, then display, you have a 100 users. Hooray. Else, that's falsy.

06:25 Okay? The array length is zero, so that's falsy. So I can, work with that. It's it's really nice. And as you work through some of, especially once we get into the, the conditional statements and stuff, you'll see it's very nice, to take advantage of the truthiness and falsiness of values in JavaScript.

06:44 I really like this feature. I use it all the time. Alright. Let's talk about why create can create an account evaluates to either false or an empty string. That's what that little pipe symbol is for is it's it's either this value or it's, it's either this type or it's this type, and those are very specific types.

07:03 So the reason that this is happening is the order of operations with these ampersands and the fact that these are being coerced. So has username as a boolean? We don't actually know because it's kind of a, it's not flowing through the, our our program here that, this is any more than just a boolean. Maybe if TypeScript were instructed a little bit better, which we're not gonna get into that, but if it were instructed a little bit better, then it would know, oh, this is true, and then it would know this is true, and so the type would be a little bit nicer. But, yeah, we're gonna get either false or an empty string here.

07:38 Actually, even in that case, it would still be false or an empty string. And the reason is even if this is let's let's say first if this is false, then the whole thing evaluates to false. Okay. So that's why you get the false here. Now what are the situations where it could actually be an empty string?

07:53 Well, let's say that this is true, and then we're gonna check email. Well, we know that that is true. Okay. So we're good with that. But then we get to the password, sorry.

08:04 The email is Truthy. We get to the password. That's Falsy. And so, in the same for the same reason that we end up with this one, being, false or here, if we do an or here, this one ends up being 42, we end up evaluating the side of the, the logical operator that actually has a value. Then this side, once we by the time we get to, okay, all of these things are true, all that we have left is this empty string password.

08:38 So that's why we end up with false or, an empty string. So all of this to say, and you can tell that, like, it's a little hard for me to explain this too. Clearly, it's a little bit of a confusing order of operations and how these things evaluate to themselves. So all of this to say, I pretty much do not rely on this functionality of JavaScript at all. And instead, if I want to, like, if I wanted to get the value of password if username and email are both true, then I would literally just write if statements for that.

09:15 If I wanted to get what this is trying to do, get a boolean value, then I would either what I'd probably do is just say boolean around the whole thing, and now I just get a boolean response or, I put a boolean around each one of these. That would be a reasonable solution as well. I really like using, the boolean constructor like this. I just think it makes a lot of sense and avoids some interesting problems. In React, for example, sometimes you'll see somebody say, like, users dot length or users dot length and and whatever.

09:53 Turns out if, user's length, is zero, you are going to render zero, right there. So, anyway, not a good idea to rely on, the coercive influence or or abuse this logical operator. If you wanna get a Boolean, you should do the Boolean thing. And so there you go. Like I said, it was it's a little bit hard for me to explain.

10:18 And just as a result of that, you probably should avoid using it and, do this the easier way. Alright. I hope that was useful and helpful to you. Let's move on to the next one.