Relational Operators

OperatorMeaning
< less-than
>greater-than
<= less-than-or-equal-to
>=greater-than-or-equal-to

The relational operators perform comparisons between values and return a Boolean value:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var result1 = 5 > 3; 
        document.writeln(result1); //true 
        var result2 = 5 < 3; 
        document.writeln(result2); //false 

    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

Rules for Relational Operators:

OperandOperandResult
numbernumbernumeric comparison
stringstringcompare the character codes in the string
numberany valueconvert operand 2 to a number and then do the comparison
any valuenumberconvert operand 1 to a number and then do the comparison
objectany valuecall valueOf() and then do the comparison. If valueOf() is not available, call toString().
any valueobjectcall valueOf() and then do the comparison. If valueOf() is not available, call toString().
Booleanany valueconvert Boolean to a number and then do the comparison
any valueBooleanconvert Boolean to a number and then do the comparison

The character codes uppercase letters are lower than the character codes of lowercase letters.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var result = "B" < "a";
        document.writeln(result); //true 
        
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

To force an alphabetic result, you can convert both operands into a upper or lower case:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">

        var result = "B".toLowerCase() < "a".toLowerCase();
        document.writeln(result); //false 

    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

Comparing numbers that are strings:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        var result = "23" < "3"; 
        document.writeln(result);//true 
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The code above returns true because the character code for "2" is 50 and the character code for "3" is 51.

Comparing a string and a number:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
    
        var result = "23" < 3; 
        document.writeln(result);//false
       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

The code above returns false because "23" is converted into the number 23 and then compared to 3.

Comparing a not-able-to-convert-to-number string and a number:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
    
        var result = "a" < 3; //"a" becomes NaN 
        document.writeln(result);//false

    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

Compare with NaN:

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
    
        var result1 = NaN < 3; 
        document.writeln(result1);//false 
        var result2 = NaN >= 3; 
        document.writeln(result2);//false 

       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Operators:
  1. JavaScript Operators
  2. Increment/Decrement Operators
  3. Increment/Decrement Operators for String, Boolean, Floating-point and Object
  4. Unary Plus and Minus
  5. Bitwise Not operator
  6. Bitwise AND
  7. Bitwise OR
  8. Bitwise XOR
  9. Left Shift
  10. Signed Right Shift
  11. Unsigned Right Shift
  12. Logical NOT
  13. Logical AND
  14. Logical OR
  15. Multiply
  16. Divide
  17. Modulus
  18. Add
  19. Subtract
  20. Relational Operators
  21. Equal and Not Equal
  22. Identically Equal and Not Identically Equal
  23. Conditional Operator
  24. Assignment Operators
  25. Comma Operator