Relational Operators
Operator | Meaning |
---|---|
< | 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>
Rules for Relational Operators:
Operand | Operand | Result |
---|---|---|
number | number | numeric comparison |
string | string | compare the character codes in the string |
number | any value | convert operand 2 to a number and then do the comparison |
any value | number | convert operand 1 to a number and then do the comparison |
object | any value | call valueOf() and then do the comparison. If valueOf() is not available, call toString(). |
any value | object | call valueOf() and then do the comparison. If valueOf() is not available, call toString(). |
Boolean | any value | convert Boolean to a number and then do the comparison |
any value | Boolean | convert 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>
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>
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>
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>
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>
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>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Operators:
- JavaScript Operators
- Increment/Decrement Operators
- Increment/Decrement Operators for String, Boolean, Floating-point and Object
- Unary Plus and Minus
- Bitwise Not operator
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- Left Shift
- Signed Right Shift
- Unsigned Right Shift
- Logical NOT
- Logical AND
- Logical OR
- Multiply
- Divide
- Modulus
- Add
- Subtract
- Relational Operators
- Equal and Not Equal
- Identically Equal and Not Identically Equal
- Conditional Operator
- Assignment Operators
- Comma Operator