Comparison operators in JavaScript are used to compare two values and determine the relationship between them. They are commonly used in conditional statements, loops, and decision-making programs. Comparison operators return a Boolean value, either true or false, based on the comparison result. JavaScript provides operators for checking equality, inequality, greater than, less than, and other relationships between values.
Table of Contents
What Are Comparison Operators in JavaScript?
Comparison operators are symbols used to compare two values in JavaScript. The result of a comparison is always a Boolean value, which means the result is either true or false.
For example, the expression 10 > 5 checks whether 10 is greater than 5. Since the statement is correct, JavaScript returns true.
Syntax:
value1 operator value2
Types of Comparison Operators
JavaScript provides the following comparison operators:
| Operator | Name | Description |
|---|---|---|
| == | Equal To | Compares two values after type conversion if required. |
| === | Strict Equal To | Compares both value and data type without type conversion. |
| != | Not Equal To | Checks whether two values are different after type conversion if required. |
| !== | Strict Not Equal To | Checks whether two values are different in either value or data type. |
| > | Greater Than | Checks whether the first value is greater than the second value. |
| < | Less Than | Checks whether the first value is less than the second value. |
| >= | Greater Than or Equal To | Checks whether the first value is greater than or equal to the second value. |
| <= | Less Than or Equal To | Checks whether the first value is less than or equal to the second value. |
Equal To (==)
The equal-to operator == checks whether two values are equal. Before performing the comparison, JavaScript can convert the values to compatible data types.
Syntax:
value1 == value2
Example:
// JavaScript program to demonstrate the equal-to operator let a = 10; let b = "10"; console.log(a == b);
Output:
true
Here, 10 is a number and "10" is a string. The == operator performs type conversion before comparing them, so the result is true.
Strict Equal To (===)
The strict equal-to operator === checks both the value and the data type. It does not perform automatic type conversion.
Syntax:
value1 === value2
Example:
// JavaScript program to demonstrate the strict equal-to operator let a = 10; let b = "10"; console.log(a === b);
Output:
false
The values appear similar, but their data types are different. The first value is a number and the second value is a string. Therefore, === returns false.
Not Equal To (!=)
The not-equal operator != checks whether two values are different. JavaScript can perform type conversion before making the comparison.
Syntax:
value1 != value2
Example:
// JavaScript program to demonstrate the not-equal-to operator let a = 10; let b = 20; console.log(a != b);
Output:
true
Since 10 and 20 are different values, the result is true.
Strict Not Equal To (!==)
The strict not-equal operator !== checks whether two values are different in value or data type. It does not perform type conversion.
Syntax:
value1 !== value2
Example:
// JavaScript program to demonstrate the strict not-equal-to operator let a = 10; let b = "10"; console.log(a !== b);
Output:
true
The values have different data types. Therefore, the strict not-equal operator returns true.
Greater Than (>)
The greater-than operator > checks whether the first value is greater than the second value.
Syntax:
value1 > value2
Example:
// JavaScript program to demonstrate the greater-than operator let marks = 85; let passingMarks = 40; console.log(marks > passingMarks);
Output:
true
Since 85 is greater than 40, the result is true.
Less Than (<)
The less-than operator < checks whether the first value is smaller than the second value.
Syntax:
value1 < value2
Example:
// JavaScript program to demonstrate the less-than operator let age = 16; let requiredAge = 18; console.log(age < requiredAge);
Output:
true
Since 16 is less than 18, the comparison returns true.
Greater Than or Equal To (>=)
The greater-than-or-equal-to operator >= checks whether the first value is greater than or equal to the second value.
Syntax:
value1 >= value2
Example:
// JavaScript program to demonstrate the greater-than-or-equal-to operator let marks = 40; let passingMarks = 40; console.log(marks >= passingMarks);
Output:
true
The result is true because 40 is equal to 40.
Less Than or Equal To (<=)
The less-than-or-equal-to operator <= checks whether the first value is less than or equal to the second value.
Syntax:
value1 <= value2
Example:
// JavaScript program to demonstrate the less-than-or-equal-to operator let age = 18; let requiredAge = 18; console.log(age <= requiredAge);
Output:
true
The result is true because the two values are equal.
Comparing Strings
Comparison operators can also be used with strings. When strings are compared, JavaScript compares their characters based on their Unicode values.
Example:
// JavaScript program to demonstrate string comparison let firstName = "Apple"; let secondName = "Banana"; console.log(firstName < secondName);
Output:
true
In this example, "Apple" comes before "Banana" based on their character values, so the result is true.
String comparison is case-sensitive. Therefore, uppercase and lowercase characters can produce different comparison results.
Comparison Operators with Conditional Statements
Comparison operators are frequently used with if, else if, and else statements. They help JavaScript make decisions based on conditions.
Example:
// JavaScript program to demonstrate comparison operators with an if statement
let age = 20;
if (age >= 18) {
console.log("Eligible to vote");
} else {
console.log("Not eligible to vote");
}
Output:
Eligible to vote
Here, the >= operator checks whether the value of age is greater than or equal to 18. Since the condition is true, the if block is executed.
Complete Program Using Comparison Operators
The following program demonstrates different comparison operators in JavaScript.
// JavaScript program to demonstrate different comparison operators
let a = 10;
let b = 20;
console.log("Equal:", a == b);
console.log("Strict Equal:", a === b);
console.log("Not Equal:", a != b);
console.log("Strict Not Equal:", a !== b);
console.log("Greater Than:", a > b);
console.log("Less Than:", a < b);
console.log("Greater Than or Equal:", a >= b);
console.log("Less Than or Equal:", a <= b);
Output:
Equal: false
Strict Equal: false
Not Equal: true
Strict Not Equal: true
Greater Than: false
Less Than: true
Greater Than or Equal: false
Less Than or Equal: true
Advantages of Comparison Operators
- Decision Making: Comparison operators help JavaScript make decisions based on conditions.
- Conditional Execution: They allow specific blocks of code to execute when a condition is true.
- Data Validation: Comparison operators can be used to validate values such as age, marks, passwords, and user input.
- Loop Control: They are commonly used to determine when loops should continue or stop.
- Value Checking: They make it possible to check whether two values are equal, different, greater, or smaller.
Common Beginner Mistakes
- Using = Instead of == or ===: The = operator assigns a value, while == and === compare values.
- Confusing == and ===: The == operator can perform type conversion, while === compares both value and data type.
- Ignoring Data Types: Values such as 10 and "10" have different data types, even though they may appear similar.
- Using Incorrect Comparison Operators: Choosing > instead of >=, or < instead of <=, can produce incorrect results.
- Forgetting That String Comparison Is Case-Sensitive: Uppercase and lowercase characters can affect the result when strings are compared.
Conclusion
Comparison operators are an important part of JavaScript because they allow programs to compare values and make decisions. Operators such as ==, ===, !=, !==, >, <, >=, and <= can be used to check equality and relationships between values. Understanding the difference between loose and strict comparison is especially important for writing predictable JavaScript programs. These operators are widely used with conditional statements, loops, validation, and other programming tasks.
FAQs
1. What are comparison operators in JavaScript?2. What is the difference between == and === in JavaScript?Comparison operators are used to compare two values and return a Boolean result, either true or false.
3. What does the !== operator do in JavaScript?The == operator compares values after possible type conversion, while the === operator compares both value and data type without type conversion.
4. Can comparison operators be used with strings in JavaScript?The !== operator checks whether two values have different values or different data types.
5. What is the result of a comparison operator?Yes, comparison operators can be used with strings, and JavaScript compares strings based on their character values.
A comparison operator returns a Boolean value, which is either true or false.
0 Comments