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:

🗒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:

✅ 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.

✅ 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.

✅ 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.

✅ 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.

✅ 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.

✅ 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.

🔹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:

true if both the operands/boolean values are true, else evaluates to false

aba && b
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

🗒Note:

  • We can use multiple operands with AND operator. Like below:
  • 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:

true if any of the operands/boolean values are true, else evaluates to false

aba || b
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

🗒Note:

  • We can use multiple operands with OR operator. Like below
  • 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.

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:

  1. NOT (!)
  2. AND (&&)
  3. 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,