The Equal operators == and === are almost identical in what they do (equality) but the only difference is in how they do it.
##"==" Operator:
For instance “==” operator does type conversion whenever necessary.
So when having to variables of similar value but different type, it will convert the variables to the same type before doing any comparison.
###Example:
A string "1" and a numeric value 1 are equal for the “==” operator.
if ("1" == 1) {
// This evaluates to true
}
if ('' == '0') {
// This evaluates to *false*
}
if (0 == '') {
// Evaluates to true.
}
if (0 == '0') {
// Evaluates to true.
}
if (null == undefined) {
// Evaluates to true.
}
“===” Operator:
For “===” operator, it doesn’t do any type conversion in order to compare any value types. in other words that means the compared values/variables should have same value and same type.
So null and undefined though they refer to the same thing but they are not equal for “===” operator, '1'(string) and 1(numeric value) are not equal as well.
Example:
if ("1" === 1) {
// This evaluates to false
}
if (0 === '') {
// Evaluates to false.
}
if (0 === '0') {
// Evaluates to false.
}
if (null === undefined) {
// Evaluates to false.
}
if ('stopbyte' === 'stopbyte') {
// Evaluates to true.
}
Conclusion
The bottom line, the “===” operator is used to compare values STRICTLY.
It’s used to check whither or not the compared variables have similar values without comparing the types.
true == 1; // evaluates True, as 1 converted to boolean equals true.
false == 'false'; // evaluates True, as 'false' (string) converted to boolean equals false.
=== Operator, a.k.a Identity operator:
As the names refers to, it’s used to check whither or not the compared variables are identical, Same value, same type, and same memory reference.
'abc' === new String('abc'); // evaluates FALSE, as they have different memory references.
'true' === true; // evaluates FALSE, as they have different types.
'1' === 'true'; // evaluates FALSe, as they don't have identical types.