JavaScript Basics Pt. 14 - Comparison Operators

JavaScript
Transcript

English (Auto-generated)

Welcome back everyone to javascript basics. We are here today with part 14 where we will be talking about comparison operators. So as we dive deeper into functions and logic in javascript we need to learn about these various comparison operators now, these operators will return a bullying value. So true or false depending on the conditions you give them. And we've actually already been using some comparison operators. When we started learning about F. L statements will be using the greater than and less than science compare values to return uh information to us based on the conditions. So let's go ahead and dive into a new comparison operator. So this first one is the equality operator. And as you can see here it's represented by two equal signs. It's used if you want to compare two values and if they are equivalent, it will return true. If not it will return false. And one thing to note that's very, very important is that the equality operator is different than the assignment operator, which is one equal side against the assignment operator will assign a value to something like a variable. So if you do, if you use just one equal sign, it's not going to do a comparison check like this, It will aside something. And the equality operator also converts the data types to check for the value. So it's not super strict on the data types. You pass it, let's go ahead and head over to replicate and do an example again, go and open up your rep lit for your front end project. Whether it's a new one or a previous one you were working and head to that script file and let's dive it. So here we will do a function. It's declared function with the function keyword. Let's get this function compare parentheses, curly braces after it. And then let's compare a number. So we'll call we're going to pass in a parameter named numb and then incentive function will do an if statement. So let's say if nam is equal let's say 21. So if num equals 21 let's do turn because we do return return counselors so we can print this message to the console return you're 21. And then if it's not 21 will do return council log, you're not 21 with a sad face. Thank you. I'm gonna give this double quotes since I'm using parentheses or single quotes here. Alright. There we go and what? There we go and then down here let's go ahead and call our function compare, pass it a number. Let's pass the 21 1st and it is telling us it's taking a second to run. You are 21. Perfect. What happens if we pass it? 31? You're 31 you're not 21 and like I mentioned, the equality operator is not very strict. It's not doesn't care too much about what type of data type you pass it. So let's go ahead and pass it a string of 21 and it's gonna tell us you're 21 because again, it's not that strict. Doesn't care what type of data type you pass, as long as that value is the same. But what if you want to get strict with it? Well, that is where the strict equality operator comes in. The strict equality operator, as you can see, is represented by these three equal signs is much more strict than the regular equality operator. So the strict equality operator does not convert data types when it checks. Hence the strict part. So if the values are not the same, it's going to return false. Let's go ahead and try that out. Head back to replicate it and let's do this same uh the same function here, stay there and we'll add that third equal sign. So this is now the strict equality operator. And let's pass it that 21 as a string that we passed it last time. But now it's not working now. It's saying you're not 21 because the string, it's not an entertainer, which is what it's looking for here. So now if we pass the 21 as a manager, it's telling us you're 21. So the strict equality operator does come in handy when you are looking for something very specific and you want to make and you want to leave like as little room for error as possible. Okay, alright, moving on, next up we have the strict inequality operator. And so just like the strict equality operator. This one does not check or this one does check for data types. So the strict inequality operator checks to make sure the values are not equal. And just like the strict inequality operator, like I mentioned, it's not going to convert data types. It's not going to be as forgiving. So let's try that out. We'll use the same function that we've got here. So it's saying if so we replaced that. So now this is the strict inequality operator and checking to make sure if you're not 21 now. Well say you're not 21 but if you are, you will say you are 21 flip that frown upside down now it's a smiley face. So let's see here if we pass the 21 is an injured you are 21. Yay, but if we pass it 21 as a string again because it's not going to convert the data types, you're not 21 same thing. If we tell it we are 24. There we go. And this may seem a little silly um saying why not just do it the other way why I have this. There will be instances in javascript in programming wars sometimes checking to make sure. Alright checking to see if it's not the thing you're looking for is easier and less steps than checking to see if it is. So that's just one important thing to keep in mind mm hmm. Alright, everyone and that is it for that lesson. Thank you so much for tuning in. And uh, and the next one. We're going to cover some more comparison operators. So I will see you there. Thanks.
149 Views 0 Likes 0 Comments

Let's learn about comparison operators in JavaScript!

Comment
Leave a comment (supports markdown format)