In this movie, we're going to talk about the concepts of truthy and falsy. Now before you stop the movie, because you think this is some kind of trick or something truthy and falsy is actually a concept we need to understand. As we have begun working with conditional expressions, and determining whether something evaluates to true or false, we need to talk about truthy and falsy. Basically, what this means is what things in JavaScript evaluate to true, what things evaluate to false. There are actual values that evaluate to true or false and we want to understand what those are. Now to introduce this, let me jump to sublime.
I have some code entered that has been used in a previous movie. Basically, we define a variable We then check the variable to see if it's not equal to undefined and using the logical and not equal to No. So if both sides of this evaluate to true, then the entire thing evaluates to true. We then log out the username, other words other otherwise we log out that the user is not defined. Okay, now using the concept of falsie and truthy, we're able to do this in a much simpler way. Let me illustrate.
I'm going to delete all of that part of the condition. All I'm going to leave is username, if username. Now is this going to work? Well, what is going to do is check to see if username evaluates to true JavaScript can do that or check to see if username evaluates to false and then it will execute the statements in the if Statement accordingly. So let's see what happens. I'm going to, I'm going to save this.
Then I'm going to copy the file path from an HTML file, jump out to the browser. And let's open that up. And then also, let's display the console down at the bottom. And currently it tells us that user is not defined. So far, it seems that that is working. What if we set username equal to no?
Save it. Come out here refresh the page still indicates user is not defined. Finally, let's set this to an actual value. Save it and it works. So why is that working? Well, the reason that works is because undefined and no both Evaluate to false they are false e values.
Therefore if username is undefined, or if username is no, it becomes false. And the else part of this if statement executes, where if it has a value evaluates to true. So let's take a look at what values in JavaScript evaluate to false see it close the console and go to a new page. So falsy values. Basically we want to see what values evaluate evaluate to false, and then anything else should evaluate to true. False, so false, the boolean value false evaluates to false.
I think that one's pretty obvious the boolean value true evaluates to true. Zero evaluates to false. negative zero evaluates to false. If we were had an if statement where we just wanted To see if it was something other than zero. We could use a comparison operator to do that. But we could also just use check the variable itself in the condition because zero evaluates to false.
Okay, now this is an empty string. So it's showing two double quotes right by each other or two single quotes right by each other. Those are both empty strings and empty string evaluates to false. So if there's nothing in a string, it evaluates defaults. No evaluates defaults. undefined evaluates to false.
So knowing the concept of truthy and falsy can help minimize the amount of code you have to write in certain conditionals. You don't have to do it that way. In fact, sometimes you getting Use the JavaScript it's easier to write it out. It's easier to write out username not equal to undefined, and username not equal to no because you understand what you're doing. But as you get more used to JavaScript, the truthy and falsy values will also be just as clear to you, and it will reduce the amount of code you need to write. Alright, let's move on to the next movie.