Even if I have been programming javascript for many years, it is never too late to learn more. Should I be embarrassed not to know every aspect of javascript? Perhaps, but I want to move on here. I even want to write about it so that You and others will also get wiser.
Comparing objects and values in javascript
Here I will go through 3 different ways of comparing values, not all of them a good idea.
The first "=", not a comparison!
The first time you start coding javascript and need to compare for instance a variable with a value you probertly make a mistake of assigning a value instead of comparing, and your code will fail to work correct.
var error = true;
if (error='true') {
// This code will always be executed as "=" assigns a value!
// And after the code has execuded the value of "error" will be 'true' (a string)
} else {
// It will never enter this area
}
Equal "==", a "soft" comparison
This comparison will do conversion of the values into types which can be compared, so it will bear over for types which are not the same. An example would be:
var error = true;
if (error=='true') {
// This code will be executed as the boolean value of "error" will be
// converted into a string and then compared
} else {
// Not executed
}
Strict equal "===", a "strict" comparison
With this comparison you will be sure that the to parts in the comparison have 100% equal values.
var error = true;
if (error==='true') {
// This code will not be executed as the type is not the same
} else {
// This area will be executed as the comparison evaluates to false.
}
Conclusion
As various ways of doing comparison exists you cannot say that any of them is the perfect comparison option. I hope that you have learned a litle more on the aspect of doing comparison in javascript.