- Learn how to use comparisons and expected results in their usage
- Understand the differences between different operators
If you have any feedback related to this course, please contact us at email@example.com.
So over here, I am using the double equal sign comparison operator, to check the equality of five versus five. And I'm gonna go ahead and run this. And the result in the console to the right is true. As I mentioned earlier, whenever we use a comparison operator, the return value is gonna be a Boolean. And before running this, if I had the expectation that five should equal five, then I should also expect that the return value would be true.
Now I'm gonna add a small tweak to this comparison. I'm gonna wrap the number five in quotes converting it from a number to a string. I'm gonna run this again, and we still get a value of true to the right. Our expectation should be that this should have returned false. We have a number value of five and a string value of five, and those are two incompatible data types for comparison purposes.
So now I'm gonna remove the quotes from the string value of five and hit run again. And we can see the value of true in the console to the right, and this matches our expectations on how strict equality should work. Now, I've just covered equality comparison operators, but what about operators to gauge the differences between two values or variables?
Usually in these cases, we're comparing one value to see if it is larger or smaller than another value. And to begin, I'm gonna start with the greater than operator. I'm gonna change the triple equal sign to the greater than operator. And I'm gonna change the number five to 15 to demonstrate this. And I'm gonna go ahead and hit run, and we see that 15 is indeed greater than five with the output of being true in the console.
Now, what if I wanted to do a comparison that included both greater than or equality, equal to? I simply put an equal sign after the greater than symbol, and I will run this again, and the result is still true. And in this scenario, this would only return false if both comparison operators returned false here, the greater than and the equal sign. I'm gonna now use the operator that's the opposite of the greater than operator.
So I'm gonna replace the greater than or equal to operator with a less than operator. And I'm gonna go ahead and hit run. And to the right, we see the value of false because 15 is not less than five, but is 15 less than 15? So I'm gonna change the five to a 15 and hit run again. And we still have a false value because 15 is not less than 15.
So I'm gonna go ahead and add the equal sign to the right of the less than operator, and I will hit run again, and now to the right, we have the value of true. Now, the last set of comparison operators I'm going to discuss are the not equal to operators. And I'm gonna begin by clearing up this environment and starting with a console log.
Now I'm gonna compare the value of five to be not equal to three. And the way I wrote this, there's this exclamation point and a equal sign. And this is a combination of a logical operator, the exclamation point, which is the not operator, and the equal sign, to represent not equal to. And now I will run this. And in the console, there is a value of true because five does not equal three. And now I'm gonna change the value of three to five and run this again, and we get a value of false because five does equal five. And this check is loosely done, just like the double equal sign operator.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.