image
Introduction to JavaScript Comparison Operators
Introduction to JavaScript Comparison Operators
Difficulty
Intermediate
Duration
7m
Students
650
Ratings
5/5
Description

This practical course explores JavaScript comparison operators. You will learn what they are and how to use them to determine equality or differences between two values or variables.

Learning Objectives

  • Get a basic understanding of what JavaScript comparisons are
  • Learn how to use comparisons and expected results in their usage
  • Understand the differences between different operators

Intended Audience

This course is intended for anyone who wants to learn about JavaScript objects.

Prerequisites

Anyone with an interest in JavaScript comparison operators or who wants to improve their knowledge of JavaScript in general.

Feedback

If you have any feedback related to this course, please contact us at support@cloudacademy.com.

Transcript

Introduction to JavaScript comparison operators. Comparison operators are used to compare equality or differences between two values or variables. When used, they will return a Boolean value. Starting off with equality, JavaScript has two comparison operators for this scenario. I'm gonna start by writing console log, five, equal sign, equal sign, five.

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.

In JavaScript, the double equal sign comparison operator is for loose equality, which means that when you're comparing two values of two different data types, the interpreter is gonna try to convert one data type to match the other data type. In this case, it converted the string data type to a number data type, which led to a true result.

JavaScript does have a strict equality comparison operator. So I'm gonna add an additional equal sign to change this into the triple equal sign operator. And I'm gonna run this again. And in the console, we see a value of false, which under normal circumstances should match our expectations that a number data type should never equal a string data type.

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.

So if I wrap five in quotes and hit run, I will still get a false value. Why? Because the JavaScript interpreter is converting the string to a number. In order to do a strictly check comparison, I just need to add an additional equal sign. And now when I run this, the value of true is in the console because five does not strictly equal string value of five. And that's it. Thanks for watching Cloud Academy.

About the Author
Students
7097
Labs
24
Courses
73
Learning Paths
31

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.

Covered Topics