How to Use Comparison Operators in JavaScript

Table of contents

Understanding Comparison Operators in JavaScript
Comparison operators are essential tools in JavaScript, used to compare two values and determine relationships like equality, inequality, or size. These operators return a boolean value: true
or false
.
Common Comparison Operators
Here’s a quick overview of the primary comparison operators:
Operator | Meaning |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
== | Equal to (value only) |
!= | Not equal to |
=== | Strict equal to (value and type) |
!== | Strict not equal to |
Using Comparison Operators in Code
Example: Checking if Age is Greater than 18
To determine whether someone is older than 18:
javascriptCopyEditlet age = 23;
console.log(age > 18); // true
Here, age > 18
evaluates to true
because 23 is indeed greater than 18.
Operator: >
(Greater Than)
The >
operator checks if the left-hand side value is greater than the right-hand side. If it’s not, the result will be false
.
Example Code:
javascriptCopyEditlet age = 18;
console.log(age > 18); // false
Output:
false
Operator: >=
(Greater Than or Equal To)
This operator returns true
if the left-hand side is greater than or equal to the right-hand side.
Example Code:
javascriptCopyEditlet age = 18;
console.log(age >= 18); // true
Output:
true
Operator: <
(Less Than)
The <
operator checks if the left-hand side value is less than the right-hand side.
Example Code:
javascriptCopyEditlet age = 16;
console.log(age < 18); // true
Output:
true
Operator: <=
(Less Than or Equal To)
This operator returns true
if the left-hand side is less than or equal to the right-hand side.
Example Code:
javascriptCopyEditlet age = 18;
console.log(age <= 18); // true
Output:
true
Equality Operators
Operator: ==
(Equal To)
The ==
operator compares only the value, ignoring the type.
Example:
javascriptCopyEditlet num = 5;
let str = "5";
console.log(num == str); // true
In this case, the number 5
and the string "5"
are considered equal because their values are the same, despite having different types.
Operator: !=
(Not Equal To)
This checks if two values are not equal.
Example:
javascriptCopyEditlet num = 5;
let str = "4";
console.log(num != str); // true
Here, 5
is not equal to "4"
, so the result is true
.
Operator: ===
(Strict Equal To)
The ===
operator checks both value and type. This is useful for strict comparisons.
Example:
javascriptCopyEditlet num = 5;
let str = "5";
console.log(num === str); // false
Since num
is a number and str
is a string, their types don’t match, and the result is fa
lse
.
Operator: !==
(Strict Not Equal To)
This operator ensures that both value and type are not equal.
Example:
javascriptCopyEditlet num = 5;
let str = "5";
console.log(num !== str); // true
Even though the values match, their types differ, so the result is true
.
Important Notes
Type Coercion with
==
: When using==
, JavaScript performs type coercion. For instance:javascriptCopyEditconsole.log(5 == "5"); // true
Strict Equality with
===
: To avoid type coercion, use===
for comparisons:javascriptCopyEditconsole.log(5 === "5"); // false
String Comparisons: Comparisons work for strings too, based on lexicographical order:
javascriptCopyEditconsole.log("apple" < "banana"); // true
Final Example: Using Comparison Operators in a Conditional
Here’s how you can use comparison operators in a real-world example:
javascriptCopyEditlet age = 20;
// Check if the person is a teenager
if (age >= 13 && age <= 19) {
console.log("You're a teenager.");
} else {
console.log("You're not a teenager.");
}
Output:
You're not a teenager.
Understanding the ===
(Triple Equals) Operator in JavaScript
In JavaScript, the ===
operator is a special type of comparison operator designed for strict comparisons. It ensures that both the value and the type of the compared entities match.
If you need to compare two variables in a way that accounts for both their value and type, the ===
operator is your go-to solution.
Why Use the ===
Operator?
Unlike the ==
operator, which performs type coercion (automatically converting one data type to another to compare values), the ===
operator checks the exact value and type.
==
compares only the value.===
compares both the value and type.
Subscribe to my newsletter
Read articles from Anmol singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
