Following on from the first three parts of my “Using JavaScript Operators” in ColdFusion 8 series, where I introduced Arithmetic, Assignment and Logical Operators, it is now the turn of Comparison Operators.

In the past we have been restricted to the more ‘wordy’ operators such as those defined in the first table below.

Operator Description
IS
EQUAL
EQ
Perform a case-insensitive comparison of two values. Return True if the values are identical.
IS NOT
NOT EQUAL
NEQ
Opposite of IS. Perform a case-insensitive comparison of two values. Return True if the values are not identical.
CONTAINS Return True if the value on the left contains the value on the right.
DOES NOT CONTAIN Opposite of CONTAINS. Return True if the value on the left does not contain the value on the right.
GREATER THAN
GT
Return True if the value on the left is greater than the value on the right.
LESS THAN
LT
Opposite of GREATER THAN. Return True if the value on the left is smaller than the value on the right.
GREATER THAN OR EQUAL TO
GTE
GE
Return True if the value on the left is greater than or equal to the value on the right.
LESS THAN OR EQUAL TO
LTE
LE
Return True if the value on the left is less than or equal to the value on the right.

However, with the arrival of ColdFusion 8, the CFML scripting language has been brought into line with other major scripting languages. This change is undoubtedly good for ColdFusion as developers familiar with the ActionScript and JavaScript syntax can now more effectively code ColdFusion and vice-versa.

The following table describes the comparison operators that can be used in ColdFusion 8, albeit used in <cfscript> expressions only:

Operator Description Examples returning true1
Equal (==) If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers; if either operand is a string, the other one is converted to a string

3 == var1
"3" == var1
3 == '3'

Not equal (!=) Returns true if the operands are not equal. If the two operands are not of the same type, JavaScript attempts to convert the operands to an appropriate type for the comparison.

var1 != 4 var1 != "5"

Greater than (>) Returns true if the left operand is greater than the right operand.

var2 > var1

Greater than or equal (>=) Returns true if the left operand is greater than or equal to the right operand.

var2 >= var1 var1 >= 3

Less than (<) Returns true if the left operand is less than the right operand.

var1 < var2

Less than or equal to (<=) Returns true if the left operand is less than or equal to the right operand.

var1 <= var2
var2 <= 5

1 These examples assume that var1 has been assigned the value 3 and var2 has been assigned the value 4.

Below are just a few simple examples of the new ColdFusion operators and their use:

IS EQUAL TO

In the below example, the value of var1 is not equal to var2, therefore the comparison returns false and the else statement is processed, writing the number 4 to the screen.

<cfscript>
var1 = 3;
var2 = 4;
if (var1 == var2) //returns false
	writeOutput(var1);
else
	writeOutput(var2); //writes out 4
</cfscript>

IS NOT EQUAL TO

In the below example, the variable var1 is not equal to 4, therefore the statement returns true, writing 3 to the screen. The second if statement serves to emphasise that ColdFusion is not strictly typed. Therefore comparing var1 to an Integer or a String is possible. ColdFusion handles type conversion in the background.

<cfscript>
var1 = 3;
var2 = 4;
if (var1 != 4) //returns true
	writeOutput(var1); //writes out 3
 
if (var1 != "5") //returns true
	writeOutput(var1); //writes out 3
</cfscript>

IS GREATER THAN

In the below example, var2 is greater than var1, therefore the value of var2, 4, is printed out on screen. Changing the operator to Greater Than or Equal To (>=) would also return the same result. However, using the Less Than (<) and Less Than or Equal To (<=) operators would return false and not write a value to the screen.

<cfscript>
var1 = 3;
var2 = 4;
if (var2 > var1) //returns true
	writeOutput(var2); //writes out 4
</cfscript>