Pages

Tuesday, July 15, 2014

Identifying the difference between the “Assignment Operator” & “Equality Operator” in JavaScript


Let’s see how to get rid of malfunctioning which occur in our application because of the misuse of “=” operators.

We can use “=” in three ways. They are;
        
     1)    “= “ used as Assignment operator(Assign value for a variable)
var A=10;

     2)   “==” used as Equality operator(check equality )
if(A==10) {
                                                          // Run this
}

      3)   “===” used as Equality operator (check equality )
If(A===10){
                                                        //Run this
}

Now you may wonder what the difference is between operators used in number (2) and (3).

To have a clear idea about this let’s see an example,
var A=123;
var B=”123”;
If(A==B){
          alert(“Variable A is equal to the variable  B”);
}
else{
          alert(“Variable A is not equal to the variable  B”);

}




var A=123;
var B=”123”;
If(A===B){
          alert(“Variable A is equal to the variable  B”);
}
else{
          alert(“Variable A is not equal to the variable  B”);

}



       

So we can say “==” is used for equality (it just simply checks the similarity and does not concern about variable types and so on) and “===”use to check strict equality (used for strict equality check where it also concerns about whether the two value’s and their variable types are same )

No comments:

Post a Comment