var = != var ==

This is a public service announcement for anyone rewriting LotusScript code in Javascript (client-side or server-side) for an XPages application.

If you want to check whether your variable (x) has a value of ‘IE Stinks’, be careful to use the correct operator.

This line, will always return true in JavaScript (and not just for the obvious semantic reason):

if (x = 'IE Stinks')

This line is the correct line to use:

if (x == 'IE Stinks')

In JavaScript, the equals sign (=) is only an assignment operator, which means it assigns the value on the right to the variable on the left. Even though syntactically similar, it is a mistake to use it as a comparison operator (comparing whether one value equals another), because it will always return true.

In LotusScript, the equals sign is both a comparison operator and an assignment operator, which means we can use it in both cases. This is why it’s easy to make that mistake.

So, to recap, IE Stinks and these are the correct operators for assignment and comparison in LotusScript and JavaScript:

Assignment Comparison
LotusScript x=’IE Stinks’ if (x=’IE Stinks’)
JavaScript x=’IE Stinks’ if (x==’IE Stinks’)
Advertisements

3 responses to “var = != var ==”

  1. Brian Moore says :

    When Watson and the other Machine Overlords take over, it won’t matter if you use one or two equalities, IE Stinks will be a true statement.

    Cheers bro,
    Brian

  2. Devin Olson says :

    Ahem: if (x.equals(“IE Stinks”)) { do something here }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: