The difference between "=", "==" and "==="
In this article, I am going to tell the difference between =, == and === in javascript. I have chosen a small example to demonstrate the comparison of == and ===.
= operator
"=" is called the assignment operator which assigns whatever value you give for a variable. for example :
var a = 5 ; var b = "batman";
Any type of variable can be assigned using this operator.
== operator
"==" this operator checks whether the values given are exactly equal irrespective of the data type. This is ok when you are comparing variables of the same data type but it will create issues if by mistake you compare variables of different data types. for example :
var a=5;
var b='5';
if(a==b) {
return true;
}
else {
return false;
}
the above code will return true since the double equals operator compares without type checking.
===operator
"===" operator checks whether the values are equal and whether the values which are being compared are of the same data type. In other words, it strictly checks the data type of operands along with the values.
the above same example will yield false in this case.
This is why using "===" is always considered a best practice while comparing values. Happy Coding...๐