JavaScript – How Are Javascript Equality Operators Useful For Comparison ?

Hello Readers, CoolMonkTechie heartily welcomes you in this article.

In this article, we will learn about how are Javascript Equality Operators useful for Comparison. Comparing any two values for equality/inequality is one of the basic functionalities provided by any programming language. Each programming language provides some operators to compare all kinds of operands. JavaScript also inherits and extends the same concept. It provides two variants of operators for comparing whether two operands are equal. This article will show the following Javascript Equality Operators for Comparison with the example as below:

  • Equality (==) Operator
  • Strict Equality (===) Operator
  • InEquality (! =) Operator
  • Strict InEquality (! ==) Operator

A famous quote about learning is :

” We now accept the fact that learning is a lifelong process of keeping abreast of change. And the most pressing task is to teach people how to learn. “

So Let’s begin.


1. Equality (==) Operator

In Javascript, the ‘==’ operator is also a loose equality operator that mainly compares two values on both sides, and then return true or false.

Syntax:

operand1 == operand2

It is type coercionwhich means that the two values are compared only after attempting to convert them into the same type. If there is a type mismatch between the operands, it first converts the operands to the same type if they are not of the same type, then applies strict comparison. If both operands are objects, in this case,  JavaScript carries out a comparison of internal references, which are equal when operands refer to the same object in memory.


Equality (==) Operator–Example 1

let var1 = 5;let var2 = 5;document.write(var1 == var2);//Output will be true

In the above example, we have taken variables that are of both number type. When we compare them using the “==” operator, JavaScript will just compare the values directly and will not do any type of conversion.


Equality (==) Operator–Example 2

let var1 = 5;let var2 = '5';document.write(var1 == var2);//Output will be true

In the above example, var1 is of number type, and var2 is of string type. Before comparing both the variables, JavaScript will first convert the types of variables, so both the variables have the same type. In this example, it will convert the string variable to a number and then will make the comparison of both the numbers. So, ideally, it just compares the values of the variables and not the data type of the variable.


2. Strict Equality (===) Operator

In Javascript, the ‘===’ operator is also a triple equals or strict equality or identity operator.

Syntax:

operand1 === operand2

The result of the identity operator is true if the operands are strictly equal with no type-conversion. During this comparison, the compiler doesn’t convert the value implicitly; instead of this, it will compare both the value and data type of the operands.


Strict Equality (===) Operator–Example 1

let var1 = 5;let var2 = 5;document.write(var1 === var2);//Output will be true

In the above example, we have taken variables, both variable “var1″ and var2” of number types. Here, as both the values of variables and their data type are the same, JavaScript will return the result of comparison true.


Strict Equality (===) Operator–Example 2

let var1 = 5;let var2 = '5';document.write(var1 === var2);//Output will be false

In the above example, we have taken var1 is of number type, and var2 is of string type. Here, === operator is not only checking the value but also the data type, and as the data type of both the variables is not the same, the output will be false.


3. InEquality (! =) Operator

In Javascript, the ‘! =’ operator is known as a loose inequality operator, which mainly compares two values on both sides and then returns true if the values are not equal and false if the values are equal.

Syntax:

operand1 != operand2

Similar to the Equality operator, this operator also converts the type of both the operands if needed. If there is a type mismatch between the operands, it first converts the operands to the same type and then applies comparison.


InEquality (! =) Operator–Example 1

let var1 = 5;let var2 = 3;document.write(var1 != var2);//Output will be true

In the above case, we have taken variables, both of which are of number type. When we compare them using the “! =” operator, JavaScript will just compare the operands based on their values. In this scenario, no type-conversion happens, as both the operands are of the same type.


InEquality (!=) Operator–Example 2

let var1 = 5;let var2 = '5';document.write(var1 != var2);//Output will be false

In the above example, var1 is of number type, and var2 is of string type. Before comparing both the variables, JavaScript will first convert the data type of variables, so that both the variables have the same data type. In this example, it will convert the string variable to the number and then will make the comparison of both the numbers. So, ideally, it just compares the values of the variables and not the data type of the variable. As the value of both the variables is 5, so the “! =” operator will return result as false.


4. Strict InEquality (! ==) Operator

In Javascript, the ‘!==’ operator is known as a not double equals or strict inequality operator. If the operands are strictly unequal with no type-conversion then the identity operator results true. During this comparison, the compiler doesn’t convert the data type of operands implicitly. Instead of this, it will compare both the values as well as the data type of operands.

Syntax:

operand1 !== operand2


Strict InEquality (! ==) Operator – Example 1

let var1 = 5;let var2 = 5;document.write(var1 !== var2);//Output will be false

In the above example, we have taken variables, both variable “var1″ and var2” of number types. Here, as both the values of variables and their data type are the same, JavaScript will return, the result of the comparison is false.


Strict InEquality (! ==) Operator–Example 2

let var1 = 5;let var2 = '5';document.write(var1 !== var2);//Output will be true

In the above example, we have taken var1 is of number type, and var2 is of string type. Here, ! == operator is not only checking the value but also the data type, and as the data type of both the variables is not the same, the output will be true.

That’s all about in this article.


Conclusion

In this article, we understood about how are Javascript Equality Operators useful for Comparison. We conclude that :

  • The Equality (==) and InEquality (! =) Operators compares the operands only based on their values and ignores the data types of the operands.
  • The Strict Equality (===) and Strict InEquality (! ==) Operators compare the operands based on both values as well as data types of the operands.

Thanks for reading !! I hope you enjoyed and learned about Equality Operators concept in JavaScript. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow other useful website as below links :

The Modern JavaScript Tutorial

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

Summary
Article Name
JavaScript – How Are Javascript Equality Operators Useful For Comparison ?
Description
This article covers how Javascript Equality Operators are useful for Comparison. Find out the difference between other operators. Comparing any two values for equality/inequality is one of the basic functionalities provided by any programming language. Each programming language provides some operators to compare all kinds of operands. JavaScript also inherits and extends the same concept. It provides two variants of operators for comparing whether two operands are equal or not. This article will demonstrate the following Operators for JavaScript Comparison with the example as below: Equality ( == ) Operator Strict Equality (===) Operator InEquality ( != ) Operator Strict InEquality (!==) Operator
Author

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version