JavaScript Comparison Operators
JavaScript comparison operators are essential tools for checking conditions and making decisions in your code. They are used to evaluate whether a condition is true or false by comparing variables or values. These operators play a crucial role in logical expressions, helping to determine the equality or differences between values.
In this article, we will explore the various comparison operators available in JavaScript, understand how they work, and see examples of how to use them in different scenarios. By the end, you’ll have a solid grasp of how to utilize these operators to make your JavaScript code more dynamic and responsive.
Comparison Operators List
There are many comparison operators as shown in the table with the description.
OPERATOR NAME | USAGE | OPERATION |
---|---|---|
Equality Operator | a == b | Compares the equality of two operators |
Inequality Operator | a != b | Compares inequality of two operators |
Strict Equality Operator | a === b | Compares both the value and type of the operand |
Strict Inequality Operator | a !== b | Compares inequality with type |
Greater than Operator | a > b | Checks if the left operator is greater than the right operator |
Greater than or equal Operator | a >= b | Checks if the left operator is greater than or equal to the right operator |
Less than Operator | a < b | Checks if the left operator is smaller than the right operator |
Less than or equal Operator | a <= b | Checks if the left operator is smaller than or equal to the right operator |
1. Equality Operator (==)
The Equality operator is used to compare the equality of two operands. If equal then the condition is true otherwise false.
Example: Below example illustrates the (==) operator in JavaScript.
// Illustration of (==) operator
let val1 = 5;
let val2 = '5';
// Checking of operands
console.log(val1 == 5);
console.log(val2 == 5);
console.log(val1 == val1);
// Check against null and boolean value
console.log(0 == false);
console.log(0 == null);
Output
true true true true false
2. Inequality Operator (!=)
The Inequality Operator is used to compare the inequality of two operands. If equal then the condition is false otherwise true.
Example: Below examples illustrate the (!=) operator in JavaScript.
// Illustration of (!=) operator
let val1 = 5;
let val2 = '5';
// Checking of operands
console.log(val1 != 6);
console.log(val2 != '5');
console.log(val1 != val2);
// Check against null and boolean value
console.log(0 != false);
console.log(0 != null);
Output
true false false false true
3. Strict equality Operator (===)
The Strict equality Operator is used to compare the equality of two operands with type. If both value and type are equal then the condition is true otherwise false.
Example: Below examples illustrate the (===) operator in JavaScript.
// Illustration of (===) operator
let val1 = 5;
let val2 = '5';
// Checking of operands
console.log(val1 === 6);
console.log(val2 === '5');
console.log(val1 === val2);
// Check against null and boolean value
console.log(0 === false);
console.log(0 === null);
Output
false true false false false
4. Strict inequality Operator (!==)
The Strict inequality Operator is used to compare the inequality of two operands with type. If both value and type are not equal then the condition is true otherwise false.
Example: Below examples illustrate the (!==) operator in JavaScript.
// Illustration of (!==) operator
let val1 = 5;
let val2 = '5';
// Checking of operands
console.log(val1 !== 6);
console.log(val2 !== '5');
console.log(val1 !== val2);
// Check against null and boolean value
console.log(0 !== false);
console.log(0 !== null);
Output
true false true true true
5. Greater than Operator (>)
The Greater than Operator is used to check whether the left-side value is greater than the right-side value. If the value is greater then the condition is true otherwise false.
Example: Below examples illustrate the (>) operator in JavaScript.
// Illustration of (>) operator
let val1 = 5;
let val2 = "5";
// Checking of operands
console.log(val1 > 0);
console.log(val2 > "10");
console.log(val1 > "10");
console.log(val2 > 0);
Output
true true false true
6. Greater than or equal Operator (>=)
The Greater than or equal Operator is used to check whether the left side operand is greater than or equal to the right side operand. If the value is greater than or equal then the condition is true otherwise false.
Example: Below examples illustrate the (>=) operator in JavaScript.
// Illustration of (>=) operator
let val1 = 5;
let val2 = "5";
// Checking of operands
console.log(val1 >= 5);
console.log(val2 >= "15");
console.log(val1 >= "5");
console.log(val2 >= 15);
Output
true true true false
7. Less than Operator (<)
The Less than Operator is used to check whether the left-side value is less than the right-side value. If yes then the condition is true otherwise false.
Example: Below examples illustrate the (<) operator in JavaScript.
// Illustration of (<) operator
let val1 = 5;
let val2 = "5";
// Checking of operands
console.log(val1 < 15);
console.log(val2 < "0");
console.log(val1 < "0");
console.log(val2 < 15);
Output
true false false true
8. Less than or equal Operator (<=)
The Less than or equal Operator is used to check whether the left side operand value is less than or equal to the right side operand value. If yes then the condition is true otherwise false.
Example: Below examples illustrate the (<=) operator in JavaScript.
// Illustration of (<=) operator
let val1 = 5;
let val2 = "5";
// Checking of operands
console.log(val1 <= 15);
console.log(val2 <= "0");
console.log(val1 <= "0");
console.log(val2 <= 15);
Output
true false false true
- Google Chrome
- Firefox
- Opera
- Safari
- Microsoft Edge
- Internet Explorer
We have a complete list of Javascript operators, to check those please go through this Javascript Operators Complete reference article.
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.