In this tutorial, you will learn about the Comparison operators and Logical operators with the help of examples.
In this article, you will learn-
- 1 JavaScript Comparison Operators
- 2 Example 1: Equal to Operator
- 3 Example 2: Not Equal to Operator
- 4 Example 3: Strict Equal to Operator
- 5 Example 4: Strict Not Equal to Operator
- 6 Example 5: Greater than Operator
- 7 Example 6: Greater than or Equal to Operator
- 8 Example 7: Less than Operator
- 9 Example 8: Less than or Equal to Operator
- 10 JavaScript Logical Operators
- 11 Example 9: Logical AND Operator
- 12 Example 10: Logical OR Operator
- 13 Example 11: Logical NOT Operator
JavaScript Comparison Operators
Comparison operators are used in logical statements to decide correspondence or contrast between variables or values.
comparison operators look at two values and offer back a boolean worth: either true or false. Comparison operators are used in decision making and loops.
Operator | Description | Example |
== | Equal to: true if the operands are equal | 5==5; //true |
!= | Not equal to: true if the operands are not equal | 5!=5; //false |
=== | Strict equal to: true if the operands are equal and of the same type | 5===’5′; //false |
!== | Strict not equal to: true if the operands are equal but of different type or not equal at all | 5!==’5′; //true |
> | Greater than: true if the left operand is greater than the right operand | 3>2; //true |
>= | Greater than or equal to: true if the left operand is greater than or equal to the right operand | 3>=3; //true |
< | Less than: true if the left operand is less than the right operand | 3<2; //false |
<= | Less than or equal to: true if the left operand is less than or equal to the right operand | 2<=2; //true |
Example 1: Equal to Operator
const a = 5, b = 2, c = 'hello'; // equal to operator console.log(a == 5); // true console.log(b == '2'); // true console.log(c == 'Hello'); // false
== evaluates to true if the operands are equal.
Note: In JavaScript, == is a comparison operator, whereas = is an assignment operator. If you mistakenly use = instead of ==, you might get the unwanted result.
Example 2: Not Equal to Operator
const a = 3, b = 'hello'; // not equal operator console.log(a != 2); // true console.log(b != 'Hello'); // true
!= evaluates to true if the operands are not equal.
Example 3: Strict Equal to Operator
const a = 2; // strict equal operator console.log(a === 2); // true console.log(a === '2'); // false
=== evaluates to true if the operands are equivalent and of a similar sort. Here 2 and ‘2’ are similar numbers however the data type is unique. Furthermore, === additionally checks for the data type while comparing.
Note: The distinction among == and === is that:
== evaluates to true if the operands are equivalent, however, === evaluates to true just if the operands are equivalent and of a similar sort
Example 4: Strict Not Equal to Operator
const a = 2, b = 'hello'; // strict not equal operator console.log(a !== 2); // false console.log(a !== '2'); // true console.log(b !== 'Hello'); // true
!== evaluates to true if the operands are carefully not equivalent. It’s the direct inverse of carefully equivalent ===.
In the above example, 2 != ‘2’ gives true. This is on the grounds that their sorts are diverse despite the fact that they have a similar worth.
Example 5: Greater than Operator
const a = 3; // greater than operator console.log(a > 2); // true
>evaluates to true if the left operand is greater than the right operand.
Example 6: Greater than or Equal to Operator
const a = 3; // greater than or equal operator console.log(a >= 3); //true
>= evaluates to true if the left operand is greater than or equal to the right operand.
Example 7: Less than Operator
const a = 3, b = 2; // less than operator console.log(a < 2); // false console.log(b < 3); // true
< evaluates to true if the left operand is less than the right operand.
Example 8: Less than or Equal to Operator
const a = 2; // less than or equal operator console.log(a <= 3) // true console.log(a <= 2); // true
<= evaluates to true if the left operand is less than or equal to the right operand.
JavaScript Logical Operators
Logical operators perform logical operations: AND, OR and NOT.
Operator | Description | Example |
&& | Logical AND: true if both the operands/boolean values are true, else evaluates to false | true && false; // false |
|| | Logical OR: true if either of the operands/boolean values is true. evaluates to false if both are false | true || false; // true |
! | Logical NOT: true if the operand is false and vice-versa. | !true; // false |
Example 9: Logical AND Operator
const a = true, b = false; const c = 4; // logical AND console.log(a && a); // true console.log(a && b); // false console.log((c > 2) && (c < 2)); // false
&& evaluates to true if both the operands are true, else evaluates to false.
Note: You can likewise use logical operators with numbers. In JavaScript, 0 is false and all non-zero values are true.
Example 10: Logical OR Operator
const a = true, b = false, c = 4; // logical OR console.log(a || b); // true console.log(b || b); // false console.log((c>2) || (c<2)); // true
|| evaluates to true if either of the operands is true. If both operands are false, the result is false.
Example 11: Logical NOT Operator
const a = true, b = false; // logical NOT console.log(!a); // false console.log(!b); // true
! evaluates to true if the operand is false and vice-versa.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.