What is JavaScript Comparisons?
JavaScript comparisons allow you to compare values and determine the relationships between them. They return Boolean values (true or false) and are widely used in conditional statements, loops, and decision-making logic.
In this guide, you will learn all about JavaScript comparison operators, their applications and examples to help you understand their purpose and usage.
Types of JavaScript Comparison Operators
JavaScript provides several types of comparison operators:
1. Equality and Inequality Operators
These operators compare two values for equality or inequality.
== (Equal To)
Checks if two values are equal after type conversion. Example:
console.log(5 == "5"); // Output: true (type conversion)
console.log(5 == 6); // Output: false
=== (Strict Equal To)
Checks if two values are equal without type conversion. Example:
console.log(5 === "5"); // Output: false (no type conversion)
console.log(5 === 5); // Output: true
!= (Not Equal To)
Checks if two values are not equal after type conversion. Example:
console.log(5 != "5"); // Output: false
console.log(5 != 6); // Output: true
!== (Strict Not Equal To)
Checks if two values are not equal without type conversion. Example:
console.log(5 !== "5"); // Output: true
console.log(5 !== 5); // Output: false
2. Relational Operators
These operators compare values in terms of order.
> (Greater Than)
Returns true if the left value is greater than the right value. Example:
console.log(10 > 5); // Output: true
console.log(5 > 10); // Output: false
< (Less Than)
Returns true if the left value is less than the right value. Example:
console.log(5 < 10); // Output: true
console.log(10 < 5); // Output: false
>= (Greater Than or Equal To)
Returns true if the left value is greater than or equal to the right value. Example:
console.log(10 >= 10); // Output: true
console.log(5 >= 10); // Output: false
<= (Less Than or Equal To)
Returns true if the left value is less than or equal to the right value. Example:
console.log(5 <= 10); // Output: true
console.log(10 <= 5); // Output: false
Special Comparisons
Comparing Strings
When comparing strings, JavaScript compares their Unicode values character by character.
Example:
console.log("apple" > "banana"); // Output: false
console.log("apple" < "banana"); // Output: true
Comparing Different Types
When comparing different types, JavaScript attempts to convert them into numbers (except for strict comparisons).
Example:
console.log(5 > "2"); // Output: true ("2" is converted to 2)
console.log("abc" < 5); // Output: false ("abc" cannot be converted to a number)
Practical Uses of Comparisons
1. Conditional Statements
Comparisons are often used in if…else conditions to control the flow of the program.
Example:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
// Output: You are an adult.
2. Loops
Comparisons are used in loops to define exit conditions.
Example:
for (let i = 0; i < 5; i++) {
console.log(i);
}
// Output: 0, 1, 2, 3, 4
3. Validation
They are essential for validating user inputs or comparing values in functions.
Example:
function isPasswordValid(password) {
return password.length >= 8;
}
console.log(isPasswordValid("12345")); // Output: false
console.log(isPasswordValid("12345678")); // Output: true
Common Mistakes and Best Practices
Avoid Using == for Critical Comparisons
Use === instead of == to avoid unexpected results due to type conversion. Example:
console.log(false == 0); // Output: true (type conversion)
console.log(false === 0); // Output: false
Understand String Comparisons
Remember that string comparison is case-sensitive and based on Unicode values. Example:
console.log("apple" === "Apple"); // Output: false
Handle Null and Undefined Carefully
Be cautious when comparing null and undefined because they behave unpredictably with loose equality. Example:
console.log(null == undefined); // Output: true
console.log(null === undefined); // Output: false