Whatterz


Using JavaScript Comparison Operators in ColdFusion 8

by Simon. Average Reading Time: about 2 minutes.

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>

This article has been tagged

, , , , , , , ,

Other articles I recommend

Using JavaScript Logical Operators in ColdFusion 8

Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

Using JavaScript Assignment Operators in ColdFusion 8

In my first post in this series, I introduced Using JavaScript Arithmetic Operators in ColdFusion 8. Now we’ll concentrate on Assignment operators.

An assignment operator assigns a value to its left operand based on the value of its right operand.

The basic assignment operator is equal (=), which assigns the value of its right operand to its left operand. That is, x = y assigns the value of y to x. The other assignment operators are usually shorthand for standard operations, as shown in the following table.

Using JavaScript Arithmetic Operators in ColdFusion 8

An often mentioned complaint by ColdFusion developers is the lack of operators commonly found in other programming languages such as JavaScript. For example, instead of the greater-than (>) symbol we have been restricted to the more wordy GT or GREATER THAN operator. However, in ColdFusion 8 this has changed and we have more freedom to use familiar JavaScript operators in <cfscript> blocks.

In the following series of posts, I will introduce the changes and show some simple examples. The first in the series is Arithmetic Operators.

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value. The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).

These operators work as they do in most other programming languages.