Relational and Equality Operators | Bondar Academy
Course: JavaScript for Testers
Module: JavaScript Fundamentals
Instructor: Artem Bondar
Lesson Summary
This lesson covers relational and equality operators in programming, which are essential for comparing values and controlling application logic. Relational Operators Relational operators compare two values and return a Boolean result (true or false). The main relational operators include: more than (>) less than ( more than equal (>=) less than equal ( For example: let result = 10 > 5; // true result = 4 > 5; // false result = 4 = 5; // true result = 6 >= 5; // true result = 4 >= 5; // false These operators help determine the flow of the application based on conditions. Equality Operators Equality operators compare values and also return a Boolean result. There are two types: double equals (==) - loose comparison (only value is compared) triple equals (===) - strict comparison (value and data type are compared) For example: let x = 1; console.log(x == 1); // true (loose comparison) console.log(x === '1'); // false (strict comparison) In summary: Relational operators yield a Boolean result. Equality operators can be loose (==) or strict (===), affecting how comparisons are made. Understanding these operators is crucial for driving application logic effectively.
Video Transcript
In this lesson, we will talk about relational and equality operators. Relational operators are responsible for comparing two values. For example, is value A less than value B, or value A more than value B. And equality operators are responsible for comparing two values, if value A equal to value B. And the result of those comparisons will be true or the false statement. And based on this statement, we can drive the logic of our application. So in this lesson, I will show you how to use those operators, how to get the result of those operators and process it. So let's get into it. So let's start with the relational one. Relational or comparison operators. This is also how they call comparison. Let me list them first and I will show you examples one by one. The first operator called more than. This one called less than. This one called more than. More than equal. And less than equal. And now let me create a variable, call it result. And let's create a first relational or first comparison. So let's say I want to say that 10 is more than 5. Let's use first operator more than. So with the relational operators, we comparing what is on the left with what is on the right. So this particular operator is making comparison if the value on the left more than value on the right. And let's print the result console.log. We print result of the console and execute. We run it and what we see? We see the result as true. So the result of relational operators, this is also logical operator, will be always a Boolean. So it will gonna return true or false by confirming us if the 10 more than 5 or not. And if, for example, I will put 4 is 4 more than 5 and run this one more time, we see the result. All right, this is false because definitely 4 is not more than 5. 4 is less than 5. That's why it is false. But if I will change my operator to this one, which is called less than. In this example, we comparing is 4 less than 5. And if we run this expression and this time it shows yes, of course, it is true because 4 is less than 5. Moving on. So let's say if I would put is 5 less than 5. What do we have? The result is false. Or if I put is 5 more than 5, run it again and false. Because, of course, 5 is not more than 5 or not less than 5. 5 is equal to 5. And in this example are useful operators like more than equal or less than equal. So we're going to use expression like is 5 more than equal 5. And if I run this one more time, we see yes, true. So 5 is not more than 5, but 5 is equal 5. So that's why the result of this overall comparison expression will be true. If I will put 6 here and we'll compare is 6 more than equal 5. And if we run it, yes, of course, it's true because 6 is more than 5. But if I put 4 and execute this one more time, what do I see? I see false because 4 is less than 5. And of course, 4 is not equal to 5. And that's why this expression is evaluated in false. And the same way would be to work with less than. If I will replace this one to this sign, which is less than. And I will run it and we'll see what we have true because 4 is less than 5. If I will put 5, it's going to be true again because 5 is equal to 5. And if I will put 6, we will have false because 6 is more than 5. That's why it is false. So these comparison operators are used to drive your logic of the application flow. So based on the data that you have, you want to execute this piece of code or that piece of code, and you can drive this logic based on the comparison. Based on the different condition, the different branch of the code can be executed. And how to manage this logic, we're definitely going to talk about in the next lessons. And let's also talk about equality operators. And for the convenience, let me create a new variable. Let me call it just x and x is equal to 1. And now I print to the console two expressions right inside of the console. So I put x is equal to 1. And I put the second expression, sorry, console.log. And I will put second expression, console.log. And x is equal, equal, equal to 1. And run this code. Let's see what happened. I'm going to command this. So run this and execute. And let's see what we have. The first expression evaluated as true. The second one is in false. But what's the difference? This one is true. This one is false. But both are comparing a x is equal to 1 and this one x is equal to 1. And we know that x is equal to 1 because we assigned 1 to our variable. The difference is this one. If we use double equal, we're comparing the value on the left to the value on the right. And it's called a loose comparison. We do not compare the data type of the value. We're just comparing the value itself. This is 1 and this is 1 and they are the same. So that's why it is true. But when we use a triple equal sign, this is a strict comparison. Strict comparison means that we are not only checking the value, but we're also checking the data type of the value. In this example, 1 is a number. But in this example, 1 is a string because it is in a single quote. So this expression will be evaluated as false because 1 as a number is not equal as 1 as a string. That's why it's false. But if we will change it to this 1 equals to 1 and run it and now everything works fine. So this evaluation is evaluated in true. So this is what the difference you need to know. Double equal sign means it's a loose comparison. If value on the left is equal to the value on the right without validation of the data type. And the triple equals is more strict comparison when the value on the left should be equal by the value to the right. And the data type should be the same as the value on the left. All right. So let's quickly summarize what we learned in this lesson. So relational or comparison operators are four types. More than, less than, more than equal and less than equal. The result of the relational and comparison operators will be always a Boolean. So it will return you true or false. When you use equality operators, there are two types. Double equals is the loose comparison when we just comparing the value. And the triple equals when we validating value and the data type. And the return result also will be a Boolean. All right. Thank you, guys. And see you in the next lesson.