JavaScript Comparison and Logical Operators

By | May 8, 2021

There are two types of Operators in JavaScript Comparision and Logical operators that often used to return boolean values or test boolean values like true or false.

JS Comparison Operators

As the name suggests comparison operators are the symbols that are used to compare two values or variables. And they always return a boolean value either true or false based on the expression.

Vamware

There are 8 types of comparison operators in JavaScript

Comparison Operators Description
== Equal to
=== equal value and type
!= not equal to
!== not equal value and type
> greater than
< less than
>= greater than equal to
<= less than equal to.

Equal to Operator (==)

The equal to operator can be represented using two equal signs(==), and it return true if both the values are equal irrespective of their data type, else it returns false .

Example

let val1 = 7;      //number
let val2 = "7";    //string
let val3 = 8;      // number

val1 == val2       //true
val1 == val3        //false

Equal value and type operator (===)

The triple equal sign represents the equal value and type operator. This operator not only checks the value but also its data type. And return true if the value and data type both are the same else it returns false.

Example

let val1 = new Number(7);    // object
let val2 = 7;                // number 
let val3 =7;

val1 === val2;                //false
val1 === val3;                //true

Not equal operator (!=)

To represent a not equal to operator we can use the != symbol. It returns true if both the value are not equal else it returns false .

Example

let val1 = new Number(7);   //object
let val2 = 7;               //number 
val1 != val2;              //false

Not equal value and type operator (!==)

To represent a not equal value and type operator we can use the != symbol. It returns true if both the values and their type are not equal else it returns false .

Example

let val1 = new Number(7);  //Object
let val2 = 7;                           //number

val1 !== val2   //true

Greater than operator (>)

The greater than operator return true if the value on the left is greater than the value on the right else it retrun false.

Example

let val1 = 20;
let val2 = 30;

val1 > val2;     //false
val2 > val1;      //true

less than operator (<)

The less than operator return true if the value on the left is less than the value on the right else it retrun false .

Example

let val1 = 20;
let val2 = 30;

val1 < val2;     //true
val2 < val1;      //false

Greater than or equal to (>=)

The greater than or equal to operator return true if the value on the left is greater than or equal to the value on the right else it retrun false .

Example

let val1 = 20;
let val2 = 30;

val1 >= val2;     //false
val2 >= val1;      //true

Less than or equal to operator (<=)

The less than or equal to operator return true if the value on the left is less than or equal to the value on the right else it retrun false .

Example

let val1 = 20;
let val2 = 30;

val1 <= val2;     //true
val2 <= val1;      //false

Logical operator

The logical operator operates between two or single boolean value and also return a boolean value as a result.

There are three types of logical operators in JavaScript.

Logical Operators Description
&& AND operator
|| OR operator
! Not operator

Logical AND operator &&

The AND operator work between two boolean values or expression and it return true only if both the values or expression are true, if either of them is false it returns false .

Example

10>5 && 10 > 7;    //true
10>11 && 10 > 7;    //false

Logical OR operator ||

The OR operator work between two boolean values or expression and it return true only if any of both values or expression is true, if both of them are false it returns false .

Example

10>5 || 10 > 7;    //true
10>11 || 10 > 7;    //true
10>11  || 10>12;    //false

Logical Not Operator (!)

The NOT operator is a unary operator and it only works on a single boolean value or expression. It inverts the boolean value, if the value is true it returns false if the value is false it returns true.

Example

!(20>30);     \\true
! (60>20);     \\false

Summary

  • Both Comparision and Logical operators return a Boolean value.
  • There are 8 types of comparison operators.
  • The equal (==)to operator check if both the values are true.
  • The equal(===) type and value operator check if the type and values are the same.
  • The not equal(!=) operator check if both the values are not equal.
  • The not equal value and type(!==) operator check if both value and types are not equal.
  • The greater than(>) operator checks if the value on the left is greater than the value on the right.
  • The less than(<) operator checks if the value on the left is less than the value on the right.
  • The greater than or equal to (>=)operator check if the value on the left is greater than or equal to the value on the right.
  • The less than or equal to (<=)operator check if the value on the left is less than or equal to the value on the right.
  • The logical operator operates between two boolean values or expressions.
  • The AND (&&)operator check if both the operands are true.
  • The OR (||)operator checks if either of the operands is true.
  • The NOT(!) operator invert the boolean value.

Leave a Reply

Your email address will not be published. Required fields are marked *