How to correctly use JavaScript Comparison Operators?

ยท

3 min read

Do you get confused with '==' & '===' or '!=' & '!==' or many such operators? ๐Ÿค”.

No Worries, ๐Ÿ˜Š In this blog, we will quickly learn about JavaScript comparison operators with some interesting examples.

JavaScript Comparison Operators

Comparison operators are used to compare two values and give back a Boolean value: either true or false. They are mostly used in loops and decision making.

Example 1: Equal to Operator ==

const a = 3, b = 5, c = 'happy';

// equal to operator
console.log(a == 3);     //true eg.1
console.log(b == '5');   //true  eg.2
console.log(c == 'Happy');  //false eg.3

This operator checks whether its two operands are the same or not by changing expression from one data type to others.

In the eg.1 we see that both the variables compared are Numbers. But in eg.2 we see that b is initialized as Integer 3 but it is compared with string '3' and output is true.

From this we can learn that we can use == operator in order to compare the identity of two operands even though, they are not of a similar type.

Example 2: Not Equal to Operator != 2

const a = 3, b = 'happy';

// not equal to operator
console.log(a != 2); // true
console.log(b != 'Happy'); // true

This operator returns true if operands are not equal. This type of operators are mostly used in if-else statements where we can give condition to run some block of code if operands are not true.

Example 3: Strict Equal to Operator ===

const a = 3;

// strict equal operator
console.log(a === 3); // true
console.log(a === '3'); // false

=== evaluates to true if the operands are equal and of the same type. Here 3 and '3' are the same numbers but one is Integer and other is string so data type is different. And === also checks for the data type while comparing.

Example 4: Strict Not Equal to Operator !==

const a = 2, b = 'hello';

// strict not equal operator
console.log(a !== 2); // false
console.log(a !== '2'); // true
console.log(b !== 'Hello'); // true

!== evaluates to true if the operands are strictly not equal. It's the complete opposite of strictly equal ===.

In the above example, 2 != '2' gives true. It's because their types are different even though they have the same value.

Example 5: Greater than > and less than < operators

const a = 3;

// greater than operator
console.log(a > 2); // returns true
// less than operator
console.log(a < 5);//returns true

Here, the value of a is 3, and if we use greater than operator for 3 greater than 2 then it returns true. Similarly for less than operator if we use 3 is less than 5 then it will return true. These are most widely used operators in if-else, conditional loops etc...

Example 6: Greater than equal to >= and less than equal to<= operators

const a = 3;
const b = 2;
// greater than or equal operator
console.log(a >= 3); //true for a = 3, 4, 5...
console.log(b<=2); //true for b = 2, 1, 0...

>= evaluates to true if the left operand is greater than or equal to the right operand. <= evaluates to true if the left operand is less than or equal to the right operand.

Conclusion: Thank you for reading this blog, I hope you have liked it & this will surely clear your confusions regarding the JavaScript Comparison Operators.

ย