Difference between double equals == and triple equals === operator in javascript

Difference between double equals == and triple equals === operator in javascript

In this tutorial, you will learn the difference between double equals == and triple equals === operator in javascript.

In the comparison operators used to take decisions with if-else statements. But let's suppose we want to check two values are actually equal, instead of one being greater or less than the other. And for that, we have both double equals == and triple equals === this different equality operator is used on.


$ads={1}
 

So let's start with a simple variable 




const appleprice = 100;

use = javascript operator assigns a value for the left operand and depends on the value that should be analyzed overall the program.



if(appleprice == 100)
console.log('1kg apple price rs.100);


To make simple calculations with it. So let's now create an if statement, which will log to the console, If the person asks the apple price on the fruit stall and the stall person replies the price of the apple is rs 100,

only if the person asking is the stall person. So we can use an if statement. Now, whenever or if the block only has one line, we actually don't need two curly braces.




const appleprice = 100;
if(appleprice == 100)
console.log('1kg apple price rs.100);

Output:



1kg apple price rs.100

How does this equality operator here actually work?

If the equality operator works like a comparison operator, and equality operator will return a true or a false value.


like boolean, in above this code, true will only be the result of this operator and in case both sides are exactly the same. 

Read also:

Javascript to change the background colour font checkbox

what is domain and web hosting

How to buy best wordpress hosting plan

8 Best Free Keyword Research Tools for SEO

write a code to retrieve data from table into a datagridview in csharp


The difference between the two operators is that the double equals ==  will the loose equality operator, which compares the two equals values, loose equality operator actually does type coercion.



For Example:



const appleprice = '100';
If(appleprice == 100)
console.log('1kg apple price rs.100);

Output:




1kg apple price rs.100
console -> true


Here the above code the value '100' is true, So what this means is that this string here is '100' will be converted to a number and then the number 100 is the same as this number 100. here, the loose equality operator actually does the coercion.

If the same concept, Now we are using the triple equals  === operator, and let's see what happens

For example:



const appleprice = '100';
If(appleprice === 100)
console.log('1kg apple price rs.100);


false.


So the difference is that the triple equals === operator is called the strict equality operator. Because it does not perform the type of coercion. And so it only returns when both values are exactly the same. 


For Example:



const appleprice = 100;
If(appleprice === 100)
console.log('1kg apple price rs.100);

output:



true
1kg apple price rs.100


so, the triple equals === operator are JavaScript does not convert them for us and would not convert any value to different types and it's not the same and just simply return false.

And so that's exactly what we used here, as a condition in this if block.So don't confuse the equality =,==,=== operators.



Conclusion


Finally, to conclude the difference between double equals == and triple equals ===.  

If the double equals are called loosely operators and two equals values, the loose equality operator actually does type coercion. Whether the value is a string or number double equals == is try to convert values in different types before the compare values and

If the triple equals === are called strict equality operator. If the compare values are not the same and just simply return false. and it's not trying to convert any different types.


So sometimes we need these two operators and sometimes strictly operators. choose your need to solve any particular problem. In both cases, you need to use the strict version of the operator.

Previous Post Next Post