In this tutorial, you are going to learn about comparison and logical operators in javascript with the help of multiple examples.
Javascript Comparison Operators
Comparison operators compare two values and return a logical value: either true or false. Comparison operators are generally used in decision-making and loops.
Operator | Description | Example |
---|---|---|
== | Equal to: true if operands are equal | x == y |
=== | Strict equal to: true if operands and their types are equal | x === y |
!= | Not equal to: true if operands are not equal | x != y |
!== | Strict not equal to: true if operands are not equal and nor their types | x !== y |
> | Greater than: true if the left operand is greater than the right operand | x > y |
< | Less than: true if the right operand is greater than the left operand | x < y |
>= | Greater than or equal to: true if the right operand is greater than or equal to the left operand | x >= y |
<= | less than or equal to: true if the right operand is greater than or equal to left operand | x <= y |
Example 1: Equal to Operator
//equal to operator
console.log(1 == 1); // true
console.log('hello' == 'hello'); // true
console.log('hello' == 'Hello'); // false
console.log('1' == 1); // true
console.log(0 == false); // true
==
returns true
if the operands are equal.
Note: Sometimes we mistakenly use =
instead of ==
which can give unwanted results. So remember that =
is an assignment operator and ==
is a comparison operator.
Example 2: Strict equal to Operator
// strict equal to operator
console.log(1 === 1); // true
console.log('hello' === 'hello'); // true
console.log('1' === 1); // false
console.log(0 === false); // false
===
returns true
only if the operands and their data type are equal. For example, ‘1’ === 1
will return false because the values are the same but the types are different (‘1’
is of string type and 1
is of number type).
Example 3: Not equal to Operator
// not equal to operator
console.log(1 != 1); // false
console.log('hello' != 'hello'); // false
console.log('1' != 1); // false
console.log(0 != false); // false
console.log(3 != 2); // true
!=
return true
if the operands are not equal.
Example 4: Strict not equal to Operator
// not equal to operator
console.log(1 !== 1); // false
console.log('hello' !== 'hello'); // false
console.log('1' !== 1); // true
console.log(0 !== false); // true
!==
is the opposite of ===
. Returns true
in both conditions either if the operands are not the same or the operand types are not the same. For example, in ‘1’!== 1
the operands are the same but the types are not the same (‘1’
is of string type and 1
is of number type).
Example 5: Greater than Operator
// greater than operator
console.log(5 > 3); // true
console.log(3 > 3); // false
console.log('b' > 'a'); // true
>
returns true
if the left operand is greater than the right operand.
Example 6: Less than Operator
// less than operator
console.log(5 < 3); // false
console.log(3 < 3); // false
console.log('a' < 'b'); // true
<
returns true
if the right operand is greater than the left operand.
Example 7: Greater than or equal to Operator
// greater than or equal to operator
console.log(5 >= 3); // true
console.log(3 >= 3); // true
console.log('a' >= 'a'); // true
>=
returns true
if the left operand is greater than or equal to the right operand.
Example 8: Less than or equal to Operator
// less than or equal operator
console.log(5 <= 3); // false
console.log(3 <= 3); // true
console.log('b' <= 'b'); // true
<=
returns true
if the right operand is less than or equal to the left operand.
Javascript Logical Operators
Logical operators are generally used with boolean values and are used to perform logical operations: AND, OR, and NOT.
Name | Description | Example |
---|---|---|
&& | Logical AND: true if both operands are true , else returns false | a && b |
|| | Logical OR: true if either of the operands is true ; if both are false , returns false . | a || b |
! | Logical NOT: false if the operand is true and vice versa | !a |
Example 1: Logical AND Operator
// logical AND
const a = 3, b = -2;
console.log(a > 0 && b > 0); // false
console.log(true && true); // true
console.log(true && false); // false
&&
returns true
if both the operands are true
, else return false
.
Example 2: Logical OR Operator
// logical OR
const a = 3, b = -2;
console.log(a > 0 || b > 0); // true
console.log(true || false); // true
console.log(false || false); // false
||
returns true
if either of the operands is true
, if both the operands are false
, it returns false
.
Example 3: Logical NOT Operator
// logical NOT
console.log(!true); // false
console.log(!false); // true
console.log(!1); // false
console.log(!0); // true
!
returns true
if the operand is false
and vice versa.
FAQ
What is difference == and === in JavaScript?
==
compares and returns true
if both the operands are equal, whereas ===
compares and returns true
only if the operands are equal and have the same type. For example, "1" == 1
returns true whereas "1" === 1
returns false
.
What Javascript operator would be used to compare a value and its type?
===
is used to compare a value and its type. For Example, "1" === 1
here both values are the same but have different types, so we will get false
in return.
What are the two comparison operators for not equal in Javascript?
!=
and !==
are two not equal to comparison operators in javascript. !=
returns true
if operands are not equal, whereas !==
returns true
only if the operands and their data types are equal.