JavaScript Upgrade Gotcha #5: = does not equal ==

I run into this occasionally when converting Dynamics CRM 4.0 JavaScript into the 2011 object model.

Consider this piece of code:

if ((crmForm.all.new_field1.DataValue >= 3) &&
    (crmForm.all.new_field1.DataValue <= 10)) {
    sitetotal = 2;
} else if (crmForm.all.new_field1.DataValue > 10) {
    sitetotal = 3;
} else if (crmForm.all.new_field1.DataValue = 0) {
    sitetotal = 0;

Can you spot the error?


How about this one:

switch (crmForm.all.new_field1.DataValue) {
    case (crmForm.all.new_field1.DataValue = "1"):
    case (crmForm.all.new_field1.DataValue = "2"):


Both of these code segments are using the assignment operator, “=” instead of the equality operator, which is “==”.

This generally happens as a legitimate typo or when you are in a hurry and just don’t type the right operator or finally, because you just didn’t know the difference. Unfortunately, this code will execute without producing any errors, because it is legal JavaScript.  The problem is the compiler did what you told it to, not what you wanted it to (damn compilers…).

I think in this case, the first code segment falls into the typo, in a hurry scenario.  The second the developer clearly did not know JavaScript, upfortunately.


This type of mistake is very difficult to spot when using the “little white box” that the native Dynamics CRM JavaScript editor provides, which is why I continue to recommend that you use Visual Studio to edit your JavaScript.  What is even more troubling is these code samples came from working code-bases that have in production since 2007 or 2008 with no one detecting the issue this entire time.

Yet another thing to watch for when upgrading your JavaScript.

Leave a Reply 0 comments