Introduction
JavaScript comparison operators are used to compare two values and return a Boolean value (true or false). These operators are essential in controlling the flow of a program, especially in conditional statements like if, while, and for loops.
In this article, you’ll learn everything about JavaScript comparison operators, including types, syntax, examples, real-life use cases, common mistakes, and interview questions.
What are JavaScript Comparison Operators?
JavaScript comparison operators are symbols used to compare two values or variables. The result of a comparison is always a boolean value.
Example:
let result = 10 > 5;
console.log(result); // true
- Always use === instead of ==
- Avoid comparing different data types
- Use clear and simple conditions
- Be careful with null and undefined
- Use parentheses for clarity
What is the use of the Comparison Operator
There are lots of uses of the comparison operator
- Help in decision-making
- Control program flow
- Validate data
- Enable conditional logic
- Are widely used in real-world applications
Types of JavaScript Comparison Operators
1. Equal to (==) Operator
It is used to check if two values are equal after type conversion.
Returns true if the values are equal, otherwise false
10 == "10"; // true (number 10 is coerced to string "10")
20 == 20 // true (number 20 is coerced to number 20)
2. Strict Equal to (===)
Checks if two values are equal without type conversion. Both value and type must be the same.
10 === "10"; // false
10 === 10; // true
3. Not Equal (!=)
Checks if two values are not equal (with type conversion).
10 != "10"; // false
10 != 10; // false
4. Strict Not Equal (!==)
Compares two values for inequality without performing type conversion.
10 !== "10"; // true
10 !== 10; // false
5. Greater Than (>)
Returns true if the left value is greater than the right value.
10 > 5; // true
6. Greater Than or Equal To (>=)
Returns true if the left value is greater than or equal to the right value.
10 >= 10; // true
7. Less Than (<)
Returns true if the left value is less than the right value.
5 < 10; // true
8. Less Than or Equal To (<=)
Returns true if the left value is less than or equal to the right value.
5 <= 5; // true
9. Ternary Operator (?)
A shorthand for an if-else statement. It takes three operands: a condition, a value if true, and a value if false.
const result = (10 > 5) ? "Yes" : "No"; // "Yes"
Difference Between == and ===
| Feature | == | === |
|---|---|---|
| Type Conversion | Yes | No |
| Comparison | Value only | Value + Type |
| Recommended | ❌ No | ✅ Yes |
Example:
console.log(0 == false); // true
console.log(0 === false); // false
Note: Always prefer === to avoid unexpected results.
Comparing Different Data Types
You will see comparing Different Data types.
1. String Comparison
Strings are compared alphabetically.
Example:
console.log("apple" < "banana"); // true
2. Number and String
String is converted to a number.
Example:
console.log("5" > 3); // true
3. Boolean Comparison
Example:
console.log(true == 1); // true
4. Null and Undefined
console.log(null == undefined); // true
console.log(null === undefined); // false
Real-Life Examples
I will show you some real-life examples.
1. Login Validation
let username = "admin";
let password = "123";
if (username === "admin" && password === "123") {
console.log("Login Successful");
}
2. Age Verification
let age = 20;
if (age >= 18) {
console.log("Eligible to vote");
}
3. E-commerce Discount
let price = 1000;
if (price > 500) {
console.log("Discount applied");
}
JavaScript comparison – Interview Questions
Q 1: What are comparison operators?
Q 2: What is the difference between == and ===?
Q 3: Can objects be compared using ===?
Q 4: What does != and !== mean?
Q 5: What does > operator do?
JavaScript comparison – Objective Questions (MCQs)
Q1. Which operator checks value only?
Q2. Which operator checks value and type?
Q3. What is the result of 5 == "5"?
Q4. What is the result of 5 === "5"?
Q5. Which operator means “not equal value or type”?
Conclusion
JavaScript comparison operators are essential for decision-making and controlling program flow.
By understanding how comparison operators work, including type conversion and strict comparison, you can write safer and more predictable JavaScript code.