Hey everyone, we are back with yet another interesting article😉, in this, we will talk about the Comparison & Logical operators in JS (Javascript), these operators work on two or more values and give back a boolean value: either true or false.
Comparison operators are used in logical expressions to determine their equality or differences in variables or values using decision-making and loops.
The logical operators allow you to compare variables and do something based on the result of that comparison.
For example, if the result of the comparison is true, you perform a block of code; if it’s false you perform another block of code.
Let’s discuss the Operators by taking the example.
🔹Comparison Operators in JS
We have various types of comparison operators which help us to make decisions on the basis of true or false.
✅ Equal-to Operator (==)
This operator is used to compare the equality of two operands. If equal then the condition is true otherwise false.
For example:
1 2 3 4 5 6 7 | let a = 10, b = 20, c = 'hello'; console.log(a == 10) // true console.log(b == '20') // true console.log(c == 'Hello') // false |
🗒Note: In JavaScript, == is a comparison operator, whereas = is an assignment operator. If you mistakenly use = it instead of ==, you might get an unwanted result.
✅ Not Equal-to Operator (!=)
This operator is used to compare the inequality of two operands. If equal then the condition is false otherwise true.
For example:
1 2 3 4 5 6 7 | let a = 10, b = 20, c = 'hello'; console.log(a != 11) // true console.log(b != 20) // false console.log(c != 'Hello') // true |
✅ Strict Equal-to Operator (===)
This operator is used to compare the equality and same data type of two operands. If equal then the condition is true otherwise false.
For example: Here 5 and ‘5’ are the same numbers but the data type is different. But === also checks for the data type while comparing. So, its result is false.
1 2 3 4 5 6 | let a = 5; console.log(a === 5); // true console.log(a === '5'); // false |
✅ Strict Not Equal-to Operator (!==)
This operator is used to compare the strict inequality of two operands. If equal then the condition is false otherwise true.
1 2 3 4 5 6 | let a = 5; console.log(a !== 6); // true console.log(a !== '5'); // true |
✅ Greater than Operator (>)
This operator is used to check whether the left-side value is greater than the right-side value. It evaluates to true if the left side is greater than the right side.
1 2 3 4 5 6 | let a = 5; console.log(a > 2); // true console.log(a > 9); // false |
✅ Greater than or Equal to Operator (>=)
It is used to check whether the left-side value is greater than or equal to the right-side value. It evaluates to true if the left side is greater than or equal to the right side.
1 2 3 4 5 6 7 | let a = 5; console.log(a >= 5); // true console.log(a >= 3); // true console.log(a >= 6); // false |
✅ Less than Operator (<)
This operator is used to check whether the left-side value is less than the right-side value. It evaluates to true if the left side is less than the right side.
1 2 3 4 5 6 | let a = 5; console.log(a < 2); // false console.log(a < 9); // true |
✅ Less than or Equal to Operator (<=)
It is used to check whether the left-side value is less than or equal to the right-side value. It evaluates to true if the left side is less than or equal to the right side.
1 2 3 4 5 6 7 | let a = 5; console.log(a <= 5); // true console.log(a <= 3); // false console.log(a <= 6); // true |
🔹Logical Operators in JS
The logical operators are important in JavaScript because they allow you to compare variables and do something based on the result of that comparison.
We have 3 types of Logical operators:
✅ Logical AND Operator (&&)
JavaScript uses the double ampersand ( &&) to represent the logical AND operator. It evaluates operands from left to right.
The following expression uses the && operator:
1 2 3 | let result = a && b; |
true if both the operands/boolean values are true, else evaluates to false
a | b | a && b |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
1 2 3 4 5 6 7 8 9 | let a = true, b = false; let c = 8; console.log(a && a); // true console.log(a && b); // false console.log((c > 2) && (c < 2)); // false |
🗒Note:
- We can use multiple operands with AND operator. Like below:
1 2 3 | let result = a && b && c; |
- In other words, AND returns the first falsy value or the last value if none were found.
✅ Logical OR Operator (||)
JavaScript uses the double pipe ( ||) to represent the logical OR operator. You can apply the OR operator to two or more values of any type:
The following expression uses the || operator:
1 2 3 | let result = a || b; |
true if any of the operands/boolean values are true, else evaluates to false
a | b | a || b |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
1 2 3 4 5 6 7 8 9 | let a = true, b = false; let c = 8; console.log(a || a); // true console.log(a || b); // true console.log((c > 2) || (c < 2)); // true console.log((c == 5) || (c < 2)); // false |
🗒Note:
- We can use multiple operands with OR operator. Like below
1 2 3 | let result = a || b || c; |
- In other words, OR returns the truthy value if any of them are true.
✅ Logical NOT Operator (!)
It uses an exclamation point ! to represent the logical NOT operator. The ! operator can be applied to a single value of any type, not just a Boolean value.
! evaluates true if the operand is false and vice-versa.
1 2 3 4 5 6 | let a = true, b = false; console.log(!a); // false console.log(!b); // true |
The logical ! operator works based on the following rules:
- If ‘a’ is undefined, null, NaN, the result is true.
- If ‘a’ is an object or number other than 0, the result is false.
- In case ‘a’ is a empty string, the result is true.
🔹Logical operator precedence
When you use the mixed logical operators in an expression, the JavaScript engine evaluates the operators based on a specified order, and this order is called the operator precedence. In other words, the operator precedence is the order in which an operator is executed.
The precedence of the logical operator is in the following order from the highest to the lowest:
- NOT (!)
- AND (&&)
- OR (||)
🗒Note: You can also use logical operators with numbers. In JavaScript, 0 is false and all non-zero values are true.
So, this is all about the Comparison & Logical Operators in Javascript (JS). Hope you got to know more about it and I tried to make it easier for you with the examples.
I hope you enjoyed the article and if you found this useful, then please share it with your friends and colleagues. If this post helps you, then spread this so that other people can also benefit.
If you have any queries please feel free to post them in the comments section or anything that you want to ask through mail contact.
Thank you 😉
Also read,
- Conditional Statement in JS – with examples
- 5 Useful JavaScript/jQuery codes and techniques with examples – Must use
- Easy way to Upload the File/Image in Laravel using AJAX jQuery with example
- How to implement tooltips using CSS? Why you should use it?
What i do not understood is in reality how you’re not actually a lot more smartly-preferred than you may be now. You are very intelligent. You know thus significantly with regards to this subject, made me in my view consider it from a lot of various angles. Its like women and men are not involved unless it¦s one thing to accomplish with Woman gaga! Your individual stuffs great. Always care for it up!