Skip to content
Related Articles

Related Articles

JavaScript Comparison Operators

View Discussion
Improve Article
Save Article
  • Last Updated : 24 Mar, 2022
View Discussion
Improve Article
Save Article

In this article, we will know about various comparison operators & their implementation in Javascript. The Comparison operators are mainly used to perform the logical operations that determine the equality or difference between the values.

Operators are used to performing specific mathematical and logical computations on operands. Like C, C++, Java, Python, and various other languages, JavaScript also supports Comparison operations. Comparison operators are used in logical expressions to determine their equality or differences in variables or values. 

Example: Below is the example of the comparison operators.

Javascript




<script> 
    function gfg() { 
    let val1 = 5; 
      
    // Equality Operators
    document.write(val1 == 5);
    document.write("<br>");
      
    // Relational Operators
    document.write(val1 > 0);
    
    gfg(); 
</script>

Output:

true
true

There are various comparison operators supported by JavaScript:

  • Equality Operators
  • Relational Operators

We will discuss both the operators sequentially through the examples.

Equality Operators:

Equality (==):  This operator is used to compare the equality of two operands. If equal then the condition is true otherwise false.

Syntax:

x == y

Example 1: Below example illustrate the (==) operator in JavaScript.

Javascript




<script>
  
    // Illustration of (==) operator
    let val1 = 5;
    let val2 = '5';
  
    // Checking of operands
    console.log(val1 == 5);
    console.log(val2 == 5);        
    console.log(val1 == val1);
  
    // Check against null and boolean value
    console.log(0 == false);   
    console.log(0 == null);
</script>

Output:

> true
> true
> true
> true
> false

Example 2: This example describes the equality operator to compare the 2 object’s values.

Javascript




<script>
  
    // Illustration of (==) operator
    let obj1 = {'val1': 'value'};
    let obj2 = {'val2': 'value'};
  
    // Checking of operands
    console.log(obj1.val1 == 'value');        
    console.log(obj1 == obj2);
    console.log(obj1.val1 == obj2.val2);
  
    // Check against undefined
    console.log(0 == undefined);   
    console.log(null == undefined);
</script>

Output:

> true
> false
> true
> false
> true

Inequality(!=): This operator is used to compare the inequality of two operands. If equal then the condition is false otherwise true.

Syntax:

x != y

Example 1: Below examples illustrate the (!=) operator in JavaScript.

Javascript




<script>
  
    // Illustration of (!=) operator
    let val1 = 5;
    let val2 = '5';
  
    // Checking of operands
    console.log(val1 != 6);
    console.log(val2 != '5');        
    console.log(val1 != val1);
  
    // Check against null and boolean value
    console.log(0 != false);   
    console.log(0 != null);
</script>

Output:

> true
> false
> false
> false
> true

Example 2: This example describes the inequality operator to compare the 2 values.

Javascript




<script>
  
    // Illustration of (!=) operator
    let obj1 = {'val1': 'value'};
    let obj2 = {'val2': 'value'};
  
    // Checking of operands
    console.log(obj1.val1 != 'value');        
    console.log(obj1 != obj2);
    console.log(obj1.val1 != obj2.val2);
  
    // Check against undefined
    console.log(0 != undefined);   
    console.log(null != undefined);
</script>

Output:

> false
> true
> false
> true
> false

Strict equality(===):  This operator is used to compare the equality of two operands with type. If both value and type are equal then the condition is true otherwise false.

Syntax:

x === y

Example 1: Below examples illustrate the (===) operator in JavaScript.

Javascript




<script>
  
    // Illustration of (===) operator
    let val1 = 5;
    let val2 = '5';
  
    // Checking of operands
    console.log(val1 === 6);
    console.log(val2 === '5');        
    console.log(val1 === val1);
  
    // Check against null and boolean value
    console.log(0 === false);   
    console.log(0 === null);
</script>

Output:

> false
> true
> true
> false
> false

Example 2: This example describes the strict equality operator to compare the 2 values.

Javascript




<script>
  
    // Illustration of (===) operator
    let obj1 = {'val1': 'value'};
    let obj2 = {'val2': 'value'};
  
    // Checking of operands
    console.log(obj1.val1 === 'value');        
    console.log(obj1 === obj2);
    console.log(obj1.val1 === obj2.val2);
  
    // Check against undefined
    console.log(0 === undefined);   
    console.log(null === undefined);
</script>

Output:

> true
> false
> true
> false
> false

Please refer to the ‘===’ vs ‘==’ Comparison Operator article for the significant differences in them.

Strict inequality(!==): This operator is used to compare the inequality of two operands with type. If both value and type are not equal then the condition is true otherwise false.

Syntax:

x !== y

Example 1: Below examples illustrate the (!==) operator in JavaScript.

jscript




<script>
 // Illustration of (!==) operator
 let val1 = 5;
 let val2 = '5';
  
 // Checking of operands
 console.log(val1 !== 6);
 console.log(val2 !== '5');        
 console.log(val1 !== val1);
  
 // Check against null and boolean value
 console.log(0 !== false);   
 console.log(0 !== null);
</script>

Output:

> true
> false
> false
> true
> true

Example 2: This example describes the strict inequality operator to compare the 2 values.

Javascript




<script>
  
    // Illustration of (!==) operator
    let obj1 = {'val1': 'value'};
    let obj2 = {'val2': 'value'};
  
    // Checking of operands
    console.log(obj1.val1 !== 'value');        
    console.log(obj1 !== obj2);
    console.log(obj1.val1 !== obj2.val2);
  
    // Check against undefined
    console.log(0 !== undefined);   
    console.log(null !== undefined);
</script>

Output:

> false
> true
> false
> true
> true

Relational Operators:

Greater than operator(>):  This operator is used to check whether the left-side value is greater than the right-side value. If the value is greater then the condition is true otherwise false.

Syntax:

x > y

Example 1: Below examples illustrate the (>) operator in JavaScript.

Javascript




<script>
  
    // Illustration of (>) operator
    let val1 = 5;
    let val2 = "5";
  
    // Checking of operands
    console.log(val1 > 0);
    console.log(val2 > "10");        
    console.log(val1 > "10");
    console.log(val2 > 0);
</script>

Output:

> true
> true
> false
> true

Example 2: This example describes the greater than operator to compare the 2 values.

Javascript




<script>
  
    // Illustration of (>) operator
    let obj1 = {'val1': 1};
    let obj2 = {'val2': 3};
  
    // Checking of operands
    console.log(obj1.val1 > 0);        
    console.log(obj1 > obj2);
    console.log(obj1.val1 > obj2.val2);
    console.log(obj2 > obj1);
    console.log(obj2.val2 > obj1.val1);
</script>

Output:

> true
> false
> false
> false
> true

Greater than or equal operator(>=): This operator is used to check whether the left side operand is greater than or equal to the right side operand. If the value is greater than or equal then the condition is true otherwise false.

Syntax:

x >= y

Example 1: Below examples illustrate the (>=) operator in JavaScript.

Javascript




<script>
  
 // Illustration of (>=) operator
 let val1 = 5;
 let val2 = "5";
  
 // Checking of operands
 console.log(val1 >= 5);
 console.log(val2 >= "15");        
 console.log(val1 >= "5");
 console.log(val2 >= 15);
</script>

Output:

> true
> true
> true
> false

Example 2: This example describes the greater than or equal operator to compare the 2 values.

Javascript




<script>
  
    // Illustration of (>=) operator
    let obj1 = {'val1': 1};
    let obj2 = {'val2': 3};
  
    // Checking of operands
    console.log(obj1.val1 >= 0);        
    console.log(obj1 >= obj2);
    console.log(obj1.val1 >= obj2.val2);
    console.log(obj2 >= obj1);
    console.log(obj2.val2 >= obj1.val1);
</script>

Output:

> true
> true
> false
> true
> true

Less than operator(<): This operator is used to check whether the left-side value is less than the right-side value. If yes then the condition is true otherwise false.

Syntax:

x < y

Example 1: Below examples illustrate the (<) operator in JavaScript.

Javascript




<script>
  
 // Illustration of (<) operator
 let val1 = 5;
 let val2 = "5";
  
 // Checking of operands
 console.log(val1 < 15);
 console.log(val2 < "0");        
 console.log(val1 < "0");
 console.log(val2 < 15);
</script>

Output:

> true
> false
> false
> true

Example 2: This example describes the Less than operator to compare the 2 values.

Javascript




<script>
  
    // Illustration of (<) operator
    let obj1 = {'val1': 1};
    let obj2 = {'val2': 3};
  
    // Checking of operands
    console.log(obj1.val1 < 10);        
    console.log(obj1 < obj2);
    console.log(obj1.val1 < obj2.val2);
    console.log(obj2 < obj1);
    console.log(obj2.val2 < obj1.val1);
</script>

Output:

> true
> false
> true
> false
> false

Less than or equal operator(<=): This operator is used to check whether the left side operand value is less than or equal to the right side operand value. If yes then the condition is true otherwise false.

Syntax:

x <= y

Example 1: Below examples illustrate the (<=) operator in JavaScript.

Javascript




<script>
  
    // Illustration of (<=) operator
    let val1 = 5;
    let val2 = "5";
  
    // Checking of operands
    console.log(val1 <= 15);
    console.log(val2 <= "0");        
    console.log(val1 <= "0");
    console.log(val2 <= 15);
</script>

Output:

> true
> false
> false
> true

Example 2: This example describes the Less than or equal to an operator to compare the 2 values.

Javascript




<script>
  
    // Illustration of (<=) operator
    let obj1 = {'val1': 1};
    let obj2 = {'val2': 3};
  
    // Checking of operands
    console.log(obj1.val1 <= 10);        
    console.log(obj1 <= obj2);
    console.log(obj1.val1 <= obj2.val2);
    console.log(obj2 <= obj1);
    console.log(obj2.val2 <= obj1.val1);
</script>

Output:

> true
> true
> true
> true
> false

Supported Browsers: The browsers supported by all JavaScript Comparison operators are listed below:

  • Google Chrome
  • Firefox
  • Opera
  • Safari
  • Microsoft Edge
  • Internet Explorer

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!