Stopbyte

The Difference Between == and === Equality Operators in Javascript?

I was building a website using Javascript and I remember using == operator and some other times I use ===.

When comparing the results I find out that I get sometimes a big difference between results which I can’t understand.

  • So what is the difference between == and === equal operators in Javascript?

  • Is there any difference at all or is it just my code that is not stable enough?

thanks in advance

4 Likes

Nice question, i am curios about it too, hope you get some answer.
Will be following this question…to get updated with latest posts. :slight_smile:

2 Likes

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.

3 Likes

== Operator, a.k.a Equality operator:

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.
2 Likes

I believe that’s enough description for both == and ===, but I summarized what you said a bit at the post below.

1 Like

Yep, your sum up makes sense, thanks a lot @afree.

It has been answered @IT_Soft by @KunniCan :slight_smile: On the selected answer

hope you find that answer satisfying as I did.

1 Like