JavaScript provides several comparison operators that allow you to compare values and determine the relationship between them. These operators are used to make decisions in your code by evaluating whether a certain condition is true or false.
In this article, we will discuss the JavaScript comparison and logical operators and provide examples for each operator.
JavaScript Comparison and Logical Operators With Examples:
1. Not equal (!==) operator:
The not equal operator is represented by !== and it compares two values to check if they are not equal. The not equal operator returns true if the values being compared are not equal, and false if they are equal. For example, the following code uses the not equal operator to check if the value of the variable x is not equal to the value of the variable y:
let x = 5; let y = 10; if (x !== y) { console.log("x is not equal to y"); }
It will output “x is not equal to y”
2. Equal (==) operator:
The equal operator is represented by == and it compares two values to check if they are equal. The equal operator returns true if the values being compared are equal, and false if they are not equal. For example, the following code uses the equal operator to check if the value of the variable x is equal to the value of the variable y:
let x = 5; let y = 5; if (x == y) { console.log("x is equal to y"); }
It will output “x is equal to y”
3. Greater than (>) operator:
The greater than operator is represented by > and it compares two values to check if the first value is greater than the second value. The greater than operator returns true if the first value is greater than the second value, and false if it is not. For example, the following code uses the greater than operator to check if the value of the variable x is greater than the value of the variable y:
let x = 10; let y = 5; if (x > y) { console.log("x is greater than y"); }
It will output “x is greater than y”
4. Less than (<) operator:
The less than operator is represented by < and it compares two values to check if the first value is less than the second value. The less than operator returns true if the first value is less than the second value, and false if it is not. For example, the following code uses the less than operator to check if the value of the variable x is less than the value of the variable y:
let x = 5; let y = 10; if (x < y) { console.log("x is less than y"); }
It will output “x is less than y”
5. Greater than or equal to (>=) operator:
The greater than or equal to operator is represented by >= and it compares two values to check if the
first value is greater than or equal to the second value. The greater than or equal to operator returns true if the first value is greater than or equal to the second value, and false if it is not. For example, the following code uses the greater than or equal to operator to check if the value of the variable x is greater than or equal to the value of the variable y:
let x = 10; let y = 5; if (x >= y) { console.log("x is greater than or equal to y"); }
It will output “x is greater than or equal to y”
6. Less than or equal to (<=) operator:
The less than or equal to operator is represented by <= and it compares two values to check if the first value is less than or equal to the second value. The less than or equal to operator returns true if the first value is less than or equal to the second value, and false if it is not. For example, the following code uses the less than or equal to operator to check if the value of the variable x is less than or equal to the value of the variable y:
let x = 5; let y = 5; if (x <= y) { console.log("x is less than or equal to y"); }
It will output “x is less than or equal to y”
Conclusion:
JavaScript provides several comparison operators that allow you to compare values and determine the relationship between them. Understanding how to use javascript Comparison and Logical Operators for making decisions in your code.
The not equal (!==), equal (==), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=) operators are commonly used in JavaScript. By using these operators in combination with if-else or switch statements, you can control the flow of your code and make it more dynamic.