Forms – More About JavaScript Types

Data types are implicit and (to a degree) contextual in JavaScript, which makes life easy

on the programmer, for the most part. But there are times when it is helpful or even

essential to know whether two variables are of the same type. In recognition of this, the

folks who certify and modify the core language have introduced a new operator (or pair of

operators) that, while not in the version of JavaScript used in Acrobat 4.0, will without a

doubt be in the next version of Acrobat JavaScript. I am talking, of course, about the

strict-equality operator. Some people call it the ‘identity operator,’ but I prefer the term

strict equality, which to me is just as strict (!) and more descriptive since it

admits the possibility of a not-so-strict equaity operator (which is in fact what we’re all

accustomed to using now).

In JavaScript 1.3 and 1.4, the strict equality operator looks like three equal signs in a

row. You can use it to test things like:

if (1 === '1')   app.beep(0);

In real life, you wouldn’t test a statement like that one, necessarily. I chose it

because it illustrates a point. The point is that currently, if you do:

if (1 == '1')   app.beep(0);

you will get a beep, because with the less-strict equality operator (that we’re all

accustomed to using), the comparison is permissively type-resolved. That is,

JavaScript converts the string to a number and then does the comparison. Whereas, with the

strict-equality version (further above), the comparison fails because the items being

compared are of different types. Naturally, there is also a strict inequality

operator, !==, which can be used in much the same way and circumstances as the strict

equality operator. Again, if the parameters differ in type, !== will return true on that

basis alone.

So, if you want to compare two items by value, just use == or != per usual. If you want

to compare them by value and type, use === or !==. But don’t use these latter two yet,

because they don’t work in Acro 4. (You’ll get a syntax error.)

You May Also Like

About the Author: Kas Thomas

Leave a Reply