# Javascript - Operator Relational Operators

## Introduction

Relational Operators are

• less-than (<),
• greater-than (>),
• less-than-or-equal-to (<=), and
• greater-than-or-equal-to (>=).

The relational operators perform comparisons between values.

Each of these operators returns a Boolean value, as in this example:

```var result1 = 5 > 3;    //true
var result2 = 5 < 3;    //false
```

Here is the rules.

Operand
Result
If the operands are numbers
perform a numeric comparison.
If the operands are strings
compare the character codes of each corresponding character in the string.
If one operand is a number
convert the other operand to a number and perform a numeric comparison.
If an operand is an object

call valueOf() and use its result to perform the comparison according to the previous rules.
If valueOf() is not available, call toString() and use that value according to the previous rules.
If an operand is a Boolean
convert it to a number and perform the comparison.

For strings, characters are compared numerically compared.

The character codes of uppercase letters are all lower than the character codes of lowercase letters:

```var result = "Black" < "abc";  //true
```

"Black" is less than the string "abc", because the letter B has a character code of 66 and the letter a has a character code of 97.

To force a true alphabetic result, convert both operands into a common case and then compare like this:

```var result = "Black".toLowerCase() < "abc".toLowerCase();  //false
```

Consider the following:

```var result = "23" < "3";  //true
```

This code returns true because both operands are strings, they are compared by their character codes.

The character code for "2" is 50; the character code for "3" is 51.

If one of the operands is changed to a number, the result makes more sense:

```var result = "23" < 3;    //false
```

"23" is converted into the number 23 and then compared to 3.

Whenever a number is compared to a string, the string is converted into a number and then numerically compared to the other number.

Consider this example:

```var result = "a" < 3;    //false because "a" becomes NaN
```

The letter "a" can't be meaningfully converted into a number, so it becomes NaN.

The result of any relational operation with NaN is false, which is interesting when considering the following:

```var result1 = NaN < 3;    //false
var result2 = NaN >= 3;   //false
```