Comparision Operator

In programming language it is very frequent that we need to execute some statements based on true or false. But how would you determine something if true or false. Comparision Operators are used to determine if something is true or false. Here are common Comparision Operator available almost in every programming language -

You might already know all of the above except for last three operator. The first four operator are tought in school.

Greater Than

This operator returns true if the first operand is bigger than the second one. Other wise it returns false.

let a = 20;
let b = 50;
if(a>b){
	console.log("This will never be printed as the condition becomes false.")
}

Here, a>b, which is 20>50 evaluates to false. In other words, the > operator returns false as 20 is not greater than 50.

if(b>a){
	console.log("Got you");
}

In the above example b>a evaluates to true, hence the string "Got you" gets printed on the console.

let a = 30;
let b = 20;
a>30               // false. As a is equals to 30
a>b                // 30>a -- true

Less Than

This operator returns true if the first operand is less than the second operand. Otherwise it returns false.

let a = 10;
let b = 15;
let c = 5;
if(a<b){   // true
	console.log("Got you");
}
if(a<c){   // false
	console.log("You missed me");
}

Greater Than or Equals To

This operator returns true if the first operand is either bigger than the second operand or they are both equal. If the first operand is less than the second operand, it returns false.

let a = 30;
let b = 50;
let c = 30;
let d = 15;
a>=b          // false
a>=c          // true
a>=d          // true

Less Than or Equals To

This operator returns true if the first operand is less than the second operand or they are both equal. If the first operand is bigger than the second operand, it returns false.

let a = 30;
let b = 50;
let c = 30;
let d = 15;
a<=b          // true
a<=c          // true
a<=d          // false

Equals To

This operator returns true, if the first operand is same as the second operand. If they are not equal, it returns false.

let a = 10;
let b = 20;
let c = 30;
let d = 20;
let name = "Santanu";
let anotherName = "Santanu";
a == b          // false
b == c          // false
c == d          // false
b == d          // true
name == anotherName    // true
name == "Santanu"      // true
name == "Santnau"      // false
"Snatanu" == anotherName         // false
true == true              // true
false == false            // true
true == false             // false
false == true             // false

Not Equals To

This operator returns false if the both operand is equals. If they are not equal, this operator returns true. So it is the opposite of Equals To operator.

let a = 10;
let b = 20;
let c = 30;
let d = 20;
let name = "Santanu";
let anotherName = "Santanu";
a != b          // true
b != c          // true
c != d          // true
b != d          // false
name != anotherName    // false
name != "Santanu"      // false
name != "Santnau"      // true
"Snatanu" != anotherName         // true
true != true              // false
false != false            // false
true != false             // true
false != true             // true

Strict Equals To

This comparision operator is same as Equals to Operator but with one exception. It just doesn't check the value. It also compare their data types. If their values are equals and also their data types are also equals, then this operator returns true. Otherwise it returns false. Even if the values are same but their data types do not match, it returns false.

It is mostly used to check class objects, if both are of same type and they refer to same instance. For primitive type, we generally don't use that. But there might comes a situation when strict comparison is useful. For example in javascript -

"30" == 30            // true
"30" === 30           // false

The Strict Equals To operator returns false as the first operand is string and second one is Number. In the first comparison, "30" == 30, the string is converted to Number first and then the comparision is performed. But in Strict comparision mode, no automatic type conversion happens, and hence the result also depends on their data types too.

Points To Remember

String Comparision : “lexicographical” order

Some programming language allows you to compare string values, like Javascript and PHP.

To see which string is greater than the other, the so-called “dictionary” or “lexicographical” order is used. In other words, strings are compared letter-by-letter.

alert( 'Z' > 'A' ); // true
alert( 'Glow' > 'Glee' ); // true
alert( 'Bee' > 'Be' ); // true

The algorithm to compare two strings is simple:

In the example above, the comparison 'Z' > 'A' gets the result at the first step.

Strings "Glow" and "Glee" are compared character-by-character:

Not a real dictionary, but Unicode order

The comparison algorithm given above is roughly equivalent to the one used in book dictionaries or phone books. But it’s not exactly the same.

For instance, case matters. A capital letter "A" is not equal to the lowercase "a". Which one is greater? Actually, the lowercase "a" is. Why? Because the lowercase character has a greater index in the internal encoding table (Unicode).