Skip to content
Related Articles

Related Articles

JavaScript Comparison Operators

Improve Article
Save Article
  • Last Updated : 28 Nov, 2022
Improve Article
Save Article

The Comparison operators are mainly used to perform the logical operations that determine the equality or difference between the values.

Operators are used for 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. We will discuss them sequentially through the examples.

Comparison Operators

Equality (==) Inequality(!=) Strict equality(===) Strict inequality(!==)
Greater than operator(>) Greater than or equal operator(>=) Less than operator(<) Less than or equal operator(<=)

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 != val2);
  
    // 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 === val2);
  
    // Check against null and boolean value
    console.log(0 === false);   
    console.log(0 === null);
</script>


Output:

false
true
false
false
false

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

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.

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 !== val2);
  
 // Check against null and boolean value
 console.log(0 !== false);   
 console.log(0 !== null);
</script>


Output:

true
false
true
true
true

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

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

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

We have a complete list of JavaScript operators, to check those please go through this Javascript Operator complete Reference article.

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
Related Articles

Start Your Coding Journey Now!