JavaScript

How to use Comparison Operators in JavaScript

While programming in JavaScript, we often encounter situations where we have to compare two values before executing the next statement. For instance, you are writing a program to check if a person’s age is greater than or equal to “20”. This statement can be specified as an expression with the help of Comparison Operators in JavaScript.

Comparison operators are used to compare two values based on the added condition, and after performing the comparison, they return a boolean value, either “true” or “false”.

This write-up will discuss the usage of Comparison operators in JavaScript. So, let’s start!

Types of Comparison operators in JavaScript

In JavaScript, Comparison operators are divided into two categories: “Equality Operators” and “Rational Operators”:

Equality Operators: The Equality operators return a Boolean value if two operands are equal. The set of the Equality operators includes:

Rational Operators: Rational operators determine the relationship between two operands and return a boolean value after comparison. The set of Rational Operators includes:

  • Greater than operator (>)
  • Less than operator (<)
  • Greater than or equal operator (>=)
  • Less than or equal operator (<=)

We will explain the usage of each of the above-mentioned comparison operators in the following sections.

How to use Equality operator (==) in JavaScript

The JavaScript Equality Operator==” checks the equality of the specified operands and returns a boolean value. After converting both values to a common type, it then performs the comparison.

Syntax of Equality operator (==) in JavaScript

x == y

Here, the equality operator “==” will compare “x” and “y” values after converting the value of “y” into the “x” operand’s data type.

Example: How to use Equality operator (==) in JavaScript

First of all, we will create three constants named “x”, “y”, and “z” having the following values:

const x = 6,

y = 13,

z = 'linuxhint';

Next, we will compare the value of constant “x” with the value “6”:

console.log(x == 6);

The equality operator returns “true” because “6” equals to the constant “x” in terms of “value” and “type”:

In the below-given example, the equality operator “==” will first convert the string “13” to the number type and then compare it with the value stored in the constant “y”:

console.log(y == '13');

After evaluating the expression “y==’13’”, the equality operator will return “true”:

Lastly, we will check the constant “z” and the string “Linuxhint” for equality:

console.log(z == 'Linuxhint');

The right side operand is already a string, so the equality operator will directly compare its value and return the results:

The given output signifies that the specified operands are not equal. As the value stored in the constant “z” is “linuxhint,” and the value with which it is compared is “Linuxhint”. So, we can conclude that while comparing strings, the “equality” operator also compares the “Characters Case”.

How to use Inequality operator (!=) in JavaScript

To compare the inequality of two operands in JavaScript, the Inequality operator!=” is used. It returns a boolean value which indicates that the specified condition is true or false.

Syntax of Inequality operator (!=) in JavaScript

x != y

Example: How to use Inequality operator (!=) in JavaScript

In the following example, the inequality operator “!=” will compare “6” with the value of the “x” constant:

console.log(x != 6);

As both of the operands are equal, the inequality operator will return “false”:

Comparing the value of “y” with the string “13” will return “true” because both values are unequal in terms of the data type:

console.log(y == '13');

Similarly, the string ‘linuxhint’ stored in the “z” constant is not equal to “Linuxhint“, because the first character is in Upper-case:

console.log(z != 'Linuxhint');

So the return case of the inequality operator “!=” will be set to “true”:

How to use Strict Equality operator (===) in JavaScript

Another operator that can be utilized to compare the equality of two operands is the Strict Equality Operator===”. The term “strict” distinguishes it from the equality operator “==“, as it strictly compares the values of the specified operands without converting them into a common type.

Syntax of Strict Equality operator (===) in JavaScript

x === y

Example: How to use Strict Equality operator (===) in JavaScript

We will now check the equality between the value of “y” and the added string “13”, using the Strict Equality operator:

console.log(y === '13');

The output prints out “false” after comparing the numeric value of the constant “y” with the string “13”:

In the other condition, the strict equality operator will check the equality between the value of “y” and a number “13”:

console.log(y === 13);

Both values are equal according to their associated data type, so the strict equality operator will mark them as equal and return a “true” boolean value:

How to use Strict Inequality operator (!==) in JavaScript

The JavaScript Strict Inequality operator (!==) validates the inequality between two operands based on their “value” and “type”. It returns “true” if both type and value are unequal; otherwise, the return case is set to “false”.

Syntax of Strict Inequality operator (!==) in JavaScript

x !== y

Example: How to use Strict Inequality operator (!==) in JavaScript

The below-given example will use the Strict Inequality operator to compare the value of the constant “y” with the string “13”:

console.log(y !== '13');

The constant “y” comprises a value of the “number” type. In contrast, the other specified operand has a “string” type value, so the strict inequality operator will declare both values as “unequal” and return “true”:

How to use Greater than operator (>) in JavaScript

This Rational operator is used for verifying if the value of the left-side operand is greater than the value of the right-side operand. If both operands satisfy the added condition, the Greater than operator will return “true“; otherwise, it prints out “false”.

Syntax of Greater than operator (>) in JavaScript

x > y

Example: How to use Greater than operator (>) in JavaScript

For the demonstration purpose, we will create a constant named “x” and initialize it with “14”:

const x = 14;

In the next step, we will utilize the Greater than operator “>” to check if the value of the “x” constant is greater than “10” or not:

console.log(x > 10);

As the number “14” is greater than the “10” value, so the Greater than operator will return “true”:

How to use Less than (<) operator in JavaScript

The Less than relational operator <” is used for verifying if the value of the left-side operand is less than the value of the right-side operand. If both operands satisfy the added condition, the Less than or equal operator will return “true“; otherwise, it prints out “false”.

Syntax of Less than operator (<) in JavaScript

x <= y

Example: How to use Less than operator (<) in JavaScript

Now, we will utilize the Less than operator to check if the value of the constant “x” is less than “10” or not:

console.log(x < 10);

After performing the comparison, the specified operator returned “false,” which indicates that the value stored in the left side operand is greater than “10”:

How to use Greater than or equal operator (>) in JavaScript

The JavaScript Greater than or equal operator>=” is used to compare the left-side value to the right-side value and check it is greater or equal to it. If both operands satisfy the added condition, the Greater than or equal operator will return “true“; otherwise, it prints out “false”.

Syntax of Greater than or equal operator (>=) in JavaScript

x >= y

Example: How to use Greater than or equal operator (>=) in JavaScript

Here, the execution of the given Greater than or equal operator “>=” will return “true” because the constant “x” contains “14”:

console.log(x >= 14);

How to use Less than or equal operator (<=) in JavaScript

The JavaScript Less than or equal operator<=” is utilized to compare the left-side value to the right-side value and check it is less or not. If both operands satisfy the added condition, the Less than operator will return “true“; otherwise, it displays “false”.

Syntax of Greater than or equal operator (<=) in JavaScript

x <= y

Example: How to use Less than or equal operator (<=) in JavaScript

With the help of the Less than or equal operator, we will execute the below-given condition:

console.log(x <= 14);

The specified relational operator will mark both values as equal and return “true”:

That was all essential information related to the usage of Comparison Operators in JavaScript. Explore them further according to your preferences.

Conclusion

Comparison operators in JavaScript compare two values based on the added condition. These JavaScript operators are divided into two categories: Equality Operators and Rational Operators. Equality Operators check if two operands are equal, whereas the Rational operators determine the relationship between the specified operands. This write-up discussed the method to use Comparison Operators in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.