JavaScript Comparison operators

Below is the example of the comparison operators.

  • Example 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • Output:

    true
    true
    

Operators are used to perform 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 statements to determine equality or difference between variables or values. There are various comparison operators supported by JavaScript:

  • Equality Operators
  • Relational Operators

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

Below examples illustrate the (==) operator in JavaScript:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> true
> true
> true
> false

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> false
> false
> false
> true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Below examples illustrate the (===) operator in JavaScript:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



> false
> true
> true
> false
> false

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> false
> true
> false
> false

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

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> false
> false
> true
> true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> false
> true
> false
> true
> true

Relational Operators

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



Syntax:

x > y

Below examples illustrate the (>) operator in JavaScript:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> true
> false
> true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> false
> false
> false
> true

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

Syntax:

x >= y

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

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> true
> true
> false

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> true
> false
> true
> true

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

Syntax:

x < y

Below examples illustrate the (<) operator in JavaScript:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> false
> false
> true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



> true
> false
> true
> false
> false

Less than or equal operator  (<=): This operator is used to checks 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

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> false
> false
> true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

> true
> true
> true
> true
> false

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

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

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.