javascript icon

When working with Operators in JavaScript are you confused by the difference between the double equals “==” and the triple equals “===” in a comparison statement? It can be a little tricky determining which one is appropriate.

Here is the difference between these two Operators:

== is “EQUALITY”
=== is “STRICT EQUALITY”

If you have the following statement:
var y = 8;
var z = ”8”;

where y is the number 8 and z is the string “8″. Let’s see what happens…

if ( y==z ) {
Alert (“YES, they are Equal”);
} else {
Alert (“NO, they are not Equal”);
}

JavaScript sees that these two variables have the same value. Using the equality symbol in this example will return “YES, they are Equal”.

if ( y===z ) {
Alert (“YES, they are Equal”);
} else {
Alert (“NO, they are not Equal”);
}

But in this example, JavaScript sees that they have the same value but are not the same type. Using the strict equality symbol in this example will return “NO, they are not Equal”.

In most cases, it will be ok to use the equality symbol. BUT be careful what you are asking…