The JavaScript comparison operator takes two operands and then compare it. The result of the comparison is a boolean value. The operands could be

- a number
- a string
- a boolean value
- or an object

## Usage and Syntax

You can use *comparison *operator anywhere within the script, however, these are more popular in conditional blocks such as *if-else, loops and so on. *

```
//comparison operator syntax
<value1> comparison operator <value2>
//output: either true or false
```

Here is a simple example,

```
//comparison operator #1
console.log(34 == 23);
//output: false
if( 3 < 4)
{
console.log(true);
}
//output: true
```

As you can see that a comparison operator can be used anywhere you want as long as you have valid operand types.

## Types of Comparison Operators

The JavaScript introduces some new operators, for example, (==) is a simple equality operator found in most of the programming languages.It only checks for the equality of values mean ( “3” == 3) is *true*, whereas (===) is a new operator in JavaScript that not only checks for equality in value, but also checks for similarity in the data type; therefore, (“3” === 3) is *false*.

Here is a list of all comparison operators in JavaScript programming.

Operator | Name | Description |

== | equality | check for equal values |

=== | strict equality | check for equal values and same data type |

!= | not equality | check if operands are not equal in values |

!== | strict not equality | check if operands are not equal in both value and type |

< | less than | check if L is less than the R operand, ( L < R ) |

<= | less than or equal to | check if L is less than or equal to the R operand ( L <=R ) |

> | greater than | check if L is greater than the R operand, ( L > R ) |

>= | greater than or equal to | check if L is greater than or equal to the R operand, (L >= R) |

Now we shall discuss each one of these operators with examples.

## Equality Operator

The equality operator takes two operands and compare it for its value. If the value does not match then returns a false, otherwise, returns a true. The data type does not matter in the comparison.

### Example #1

```
//variable declarations
var x = true;
var y = 1;
console.log(x == y);
var a = 20;
var b = "20";
console.log(a == b);
var c = 33;
var d = 23;
console.log(c == d);
```

Output #1

```
true
true
false
```

## Strict Equality Operator

The *strict equality operator *( === ) compares two operands for equal values and same type. If either ** value **or

**or**

*type***mismatches, it will return a**

*both**false*, otherwise returns a

*true*.

### Example #2

```
//variable declaration
var m = true;
var n = "1";
console.log(true === "1");
var p = 120;
var q = "120";
console.log(p === q);
var str1 = "Hello";
var str2 = "hello";
console.log(str1 === str2);
var r = "Apple";
var s = "Apple";
console.log(r === s);
```

Output #2

```
false
false
false
true
```

From the example above, it is very clear that the value and its type must match perfectly for *strict equality to be true.*

## Not Equality Operator

The not equal to operator simply compares the values and if values are equal then returns a boolean *true*, otherwise, it is *false*. This comparison operator again do not check for the type, but only values.

### Example #3

```
console.log(24 != "24");
//false
console.log(24 != 35);
//true
console.log("Hello" != "hello");
//true
```

Output #3

```
false
true
true
```

In *console.log(24 != “24”)* , the value of 24 is same in both, but the type is different. Therefore, result is *false*. In the second case, console.log(24 != 35) the numbers are clearly not the same;therefore, result is *true*.

In the third case, strings are same, but, the character code of capital H and lowercase ‘h’ has different numeric values. It is also different, and result is *true*.

In comparing values, some values are special such as NaN or ( Not-a-Number). A **NaN **is defined as a numeric value that *undefined *or *not re-presentable*.

```
console.log(NaN != NaN); //true
or
console.log(NaN == NaN); //false
```

The IEEE specifies *NaN* using special floating point representation where exponents are all *1s,* mantissa is a non-zero to represent *invalid *numbers. in JavaScript, NaN can be checked using ** isNaN() **function. Mostly, some operations give invalid numbers represented by NaN. So, we can say that it is very rare that we use NaN systematically in our scripts.

## Less Than Operator

The less than operator is only to compare two operand values. Suppose you have **x **and **y **that gives this expression – * x < y*. The less than operand compares if the value of x is less than the value of y.

*Caution*: It is not y greater than x.

### Example #4

```
//variable declarations
var x = 10;
var y = 20;
console.log(x < y); //true
console.log(y < x); //false
```

Output #3

```
true
false
```

The first case above, compares* x* with *y* for *less than* value, the result is **true **because x is *10* and y is *20*; but the reverse is not true and result is **false**.

## Less Than Or Equal To Operator

The less than or equal to is found in all programming languages and mathematics. It compares two kind of values – less than and equal values. Suppose *x *and *y *are two values, then *x *is compared with *y *for equal value or less then *y*. The result is a boolean *true *or *false*.

**Example #4**

```
//variable declarations
var x = 10;
var y = 43;
var z = 43;
console.log(x <= y); //true
console.log(y <= z); //false
```

Output #4

```
true
false
```

You can see that the <= operator can be used to check both equality or less than relationship between two operands.

## Comparing strings

While comparing strings, the comparison operators will convert the string into its numerical value and then sees less then, greater than, or equality relationship. For example,

`console.log("Hello" <= "hello"); //true `

Most modern browsers use * utf-8* codes for characters which is compatible with

*codes. The letter*

**ASCII***has a*

**‘H’***utf-8*decimal value of

*72*and the letter

**has a value of**

*‘h’**104.*

## Greater Than Operator

Similar to less than operator, the greater than *(>)* operator compares *left *and *right *operand and sees if the left operand is greater than the right. Suppose *x *and *y *are two operands, then the greater than operator is compares* x > y*, if *x *is greater value, then result is * true*, otherwise,

*.*

**false**### Example #5

```
//variable declarations
var x = 100;
var y = 40;c
onsole.log(x > y); // true
console.log(y > x); // false
```

Output #5

```
true
false
```

The left value which is *x *is greater in the first case. The result is * true*. However, the left value

*y*is smaller in the second case, therefore, you get a

*false*.

## Greater Than Or Equal To

The greater than or equal to operator is similar to less than or equal to operation. Only difference is that we try to check for greater than and equality together. If* x* is greater than or equal value to* y,* then the result is *true*. If *x* is less then *false*.

**Example #6**

```
//variable declaration
var x = 23;
var y = 23;
var z = 54;
console.log(x >= y); //true
console.log(x >= z); // false
```

Output #6

```
true
false
```

The first case, *x* is equal to *y* and the result is * true*. But, in the second case, x is not greater than z; therefore, it is

*.*

**false**,