Operator
Expressions
ColdFusion expressions consist of operands and operators. Constants and variables are operands. Operators, such as the multiplication sign, are the verbs that act on the operands; functions are a form of operator.
The simplest expression consists of a single operand with no operators. Complex expressions have multiple operators and operands. The following are all ColdFusion expressions:
12 MyVariable a++ (1 + 1)/2 "father" & "Mother" Form.divisor/Form.dividend Round(3.14159)
Operators act on the operands. Some operators, such as functions with a single argument, take a single operand. Many operators, including most arithmetic and logical operators, take two operands. The following is the general form of a two-operand expression:
Expression Operator Expression
Expressions surround the operator. Each expression can be a simple operand (variable or constant) or a subexpression consisting of more operators and expressions. Complex expressions are built up using subexpressions. For example, in the expression (1 + 1)/2, 1 + 1 is a subexpression consisting of an operator and two operands.
Operator types
ColdFusion has Five types of operators:
- Arithmetic
- Boolean
- Decision (or comparison)
- String
- Ternary
Functions also can be viewed as operators because they act on operands.
Arithmetic operators
The following table describes the arithmetic operators:
|
Description |
---|---|
+ - * / |
Basic arithmetic: Addition, subtraction, multiplication, and division. In division, the right operand cannot be zero. |
++ – |
Increment and decrement. Increase or decrease the variable by one.These operators can be used for pre-incrementing or decrementing (as in {{ x = ++ i}}), where the variable is changed before it is used in the expression. They can also be used for post-incrementing or decrementing (as in x = i+), where the value is changed after it is used in the expression. If the value of the variable i is initially 7, for example, the value of x in x = ++i is 8 after expression evaluation, but in x=i, the value of x is 7. In both cases, the value of i becomes 8.These operators cannot be used with expressions that involve functions, as in f().a. Also, you can use an expression such as -x, but ---x and +x cause errors, because their meanings are ambiguous. You can use parentheses to group the operators, as in -(--x) or (++x), however. |
+= -= *= /= %= |
Compound assignment operators. The variable on the right is used as both an element in the expression and the result variable. Thus, the expression a += b is equivalent to a = a +b. |
+ - |
Unary arithmetic: Set the sign of a number. |
MOD or % |
Modulus: Return the remainder after a number is divided by a divisor. The result has the same sign as the divisor. The value to the right of the operator should be an integer; using a non-numeric value causes an error, and if you specify a real number, ColdFusion ignores the fractional part (for example, 11 MOD 4.7 is 3). |
\ |
Integer division: Divide an integer by another integer. The result is also an integer; for example, 9\4 is 2. The right operand cannot be zero. |
^ |
Exponentiation: Return the result of a number raised to a power (exponent). Use the caret character (^) to separate the number from the power; for example, 2^3 is 8. Real and negative numbers are allowed for both the base and the exponent. However, any expression that equates to an imaginary number, such -1^.5 results in the string "-1.#IND. ColdFusion does not support imaginary or complex numbers. |
Boolean operators
Boolean, or logical, operators perform logical connective and negation operations. The operands of Boolean operators are Boolean (True/False) values. The following table describes the Boolean operators:
Operator |
Description |
---|---|
NOTor ! |
Reverse the value of an argument. For example, NOT True is False and the inverse. |
AND or && |
Return True if both arguments are True; return False otherwise. For example, True AND True is True, but True AND False is False. |
OR or || |
Return True if any of the arguments is True; return False otherwise. For example, True OR False is True, but False OR False is False. |
XOR |
Exclusive or: Return True if one of the values is True and the other is False. Return False if both arguments are True or both are False. For example, True XOR True is False, but True XOR False is True. |
EQV |
Equivalence: Return True if both operands are True or both are False. The EQV operator is the opposite of the XOR operator. For example, True EQV True is True, but True EQV False is False. |
IMP |
Implication: The statement A IMP B is the equivalent of the logical statement "If A Then B." A IMP B is False only if A is True and B is False. It is True in all other cases. |
Decision operators
The ColdFusion decision, or comparison, operators produce a Boolean True/False result. Many types of operation have multiple equivalent operator forms. For example, IS and EQ perform the same operation. The following table describes the decision operators:
Operator |
Description |
---|---|
IS EQUAL |
Perform a case-insensitive comparison of two values. Return True if the values are identical. |
IS NOT |
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 |
Return True if the value on the left is greater than the value on the right. |
LESS THAN |
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 |
Return True if the value on the left is greater than or equal to the value on the right. |
LESS THAN OR EQUAL TO |
Return True if the value on the left is less than or equal to the value on the right. |
In CFScript expressions only, you can also use the following decision operators. You cannot use them in expressions in tags. == (EQ), != (NEQ), > (GT), < (LT), >= (GTE), and <= (LTE).
Decision operator rules
The following rules apply to decision operators:
- When ColdFusion evaluates an expression that contains a decision operator other than CONTAINS or DOES NOT CONTAIN, it first determines if the data can be converted to numeric values. If they can be converted, it performs a numeric comparison on the data. If they cannot be converted, it performs a string comparison. This can sometimes result in unexpected results. For more information on this behavior, see Evaluation and type conversion issues in Data type conversion.
When ColdFusion evaluates an expression with CONTAINS or DOES NOT CONTAIN it does a string comparison. The expression A CONTAINS B evaluates to True if B is a substring of A. Therefore an expression such as the following evaluates as True:
123.45 CONTAINS 3.4 |
When a ColdFusion decision operator compares strings, it ignores the case. As a result, the following expression is True:
"a" IS "A"
When a ColdFusion decision operator compares strings, it evaluates the strings from left to right, comparing the characters in each position according to their sorting order. The first position where the characters differ determines the relative values of the strings. As a result, the following expressions are True:
"abde" LT "ac"
String operators
String operators manipulate strings of characters. The following table describes the operators:
Operator |
Description |
---|---|
& |
Concatenates strings. |
&= |
Compound concatenation. The variable on the right is used as both an element in the concatenation operation and the result variable. Thus, the expression a &= b is equivalent to a = a & b.An expression can have only one compound assignment operator. |
In a Query of Queries, you use || as the concatenation operator.
Ternary Operator
The ternary operator is a decision operator with three operands. It assigns a variable a value based on a Boolean expression. The operator has the form
(Boolean expression)? expression1 : expresson2
If the Boolean expression evaluates to true, the operator result is expression1; otherwise, it is expression2
For example
<cfset c = (a GT b)? a : b >
If a is greater than b, c is assigned the value of a; otherwise, c is assigned the value of b.
The parentheses can contain any expression that evaluates to a Boolean value, and a and b can be any valid expression. You can nest this operator inside other expressions.
Operator precedence and evaluation ordering
The order of precedence controls the order in which operators in an expression are evaluated. The order of precedence is as follows. (Some alternative names for operators, such as EQUALS and GREATER THAN OR EQUAL TO are omitted for brevity.)
^ *, / \ MOD +, - & EQ, NEQ, LT, LTE, GT, GTE, CONTAINS, DOES NOT CONTAIN, ==, !=, >, >=, <, <= NOT, ! AND, && OR, || XOR EQV IMP
To enforce a nonstandard order of evaluation, parenthesize expressions. For example:
- 6 - 3 * 2 is equal to 0
- (6 - 3) * 2 is equal to 6
You can nest parenthesized expressions. When in doubt about the order in which operators in an expression are evaluated, use parentheses to force the order of evaluation.
Using functions as operators
Functions are a form of operator. Because ColdFusion functions return values, you can use function results as operands. Function arguments are expressions. For example, the following are valid expressions:
- Rand()
- UCase("This is a text: ") & ToString(123 + 456)
Function syntax
The following table shows function syntax and usage guidelines:
Usage |
Example |
---|---|
No arguments |
Function() |
Basic format |
Function(Data) |
Nested functions |
Function1(Function2(Data)) |
Multiple arguments |
Function(Data1, Data2, Data3) |
String arguments |
Function('This is a demo')Function(This is a demo) |
Arguments that are expressions |
Function1(X*Y, Function2("Text")) |
<cfset myDate = DateFormat(Now(), "mmmm d, yyyy")>
You can use the values returned by functions directly to create more complex expressions, as in the following example:
Abs(Myvar)/Round(3.14159)
For more information on how to insert functions in expressions, see Using number signs.
Optional function arguments
Some functions take optional arguments after their required arguments. If omitted, all optional arguments default to a predefined value. For example:
- Replace("Eat and Eat", "Eat", "Drink") returns "Drink and Eat"
- Replace("Eat and Eat", "Eat", "Drink", "All") returns "Drink and Drink"
The difference in the results is because the Replace function takes an optional fourth argument that specifies the scope of replacement. The default value is "One," which explains why only the first occurrence of "Eat" was replaced with "Drink" in the first example. In the second example, a fourth argument causes the function to replace all occurrences of "Eat" with "Drink".
Expression evaluation and functions
It is important to remember that ColdFusion evaluates function attributes as expressions before it executes the function. As a result, you can use any ColdFusion expression as a function attribute. For example, consider the following lines:
<cfset myStringVar = UCase(firstVariable & " more sleep!")>
When ColdFusion server executes the second line, it does the following:
-
Identifies an expression with a string concatenation.
-
Evaluates the firstVariable variable as the string "we all need".
-
Concatenates "we all need" with the string "more sleep!" to get "we all need more sleep!".
-
Passes the string "we all need more sleep!" to the UCase function.
-
Executes the UCase function on the string argument "we all need more sleep!" to get "WE ALL NEED MORE SLEEP!".
-
Assigns the string value "WE ALL NEED MORE SLEEP!" to the variable myStringVar.
ColdFusion completes steps 1-3 before running the function.
Using multiple assignments in one expression
You can chain assignments to assign the same value to multiple variables in a single statement. This includes chain assignments for the results of an expression. The following code displays a chain assignment:
a=b=c=d*5
You can use the var operator in multiple assignments, but the variables with this operator must precede all others. For example:
//The following line is valid. var a = var b = c = d*5 //The following line is not valid. // a = b = var c = d*5
Identity operator
ColdFusion (2021 release) introduces the identity operator (===). If the two values are not of the same type, when compared, it will return false. Returns true if operands are equal in value and are of the same type.
Example 1
<cfscript> // e.g. 1 === "1" is false, but 1 === 1 is true function returntrue(){ return true } writeoutput(returntrue() == 'true'); // YES writeoutput(returntrue() == TRUE); // YES writeoutput(returntrue() === 'true'); // NO writeoutput(returntrue() === true); // YES writeoutput(returntrue() === TRUE); // YES </cfscript>
Equality/inequality operator
ColdFusion is a loosely typed language, where the engine smartly interprets the type of variable. In ColdFusion (2018, release), ColdFusion preserved the data types, however due to backward compatibility, the equality operator was still comparing two objects by its values and not by types.
In ColdFusion (2021 release), we've introduced the operators- Strict equality and Strict inequality, which solve issues related to == or EQ operator.
Example- Strict equality
<cfscript> writedump(2=="2"); // YES writedump(2==="2"); // NO writedump('yes' == 1); // YES writedump('yes' === 1); // NO writedump(false ==0); // YES writedump(false ===0);//NO </cfscript>
Example- Strict inequality
<cfscript> writedump(2!="2"); // NO writedump(2!=="2"); // YES writedump('yes' != 1); // NO writedump('yes' !== 1);// YES writedump(false !=0); // NO writedump(false !==0);//YES </cfscript>
Spread operator
In ColdFusion (2021 release), the Spread operator gives you access to an iterable object. In an iterable object, you can traverse the items inside in a sequential manner.
Use the Spread operator to access the items inside the iterable objects.
You can denote a Spread operator with a … syntax.
Syntax
...variable
Let’s start off with an example to show the Spread operator works.
<cfscript> numbers = [1,2,3]; writeDump(sum( ...numbers )); function sum(required x,required y,required z) { return x + y + z; }
Output
6
To summarize, the Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.
For example,
<cfscript> myarray1=[5,6,7] myarray2=[3,4,5,...myarray1] myarray3=[1,2,3,...myarray2] writedump(myarray3) </cfscript>
Similarly, using the Spread operator, you can duplicate object literals in a struct. Using spread, you can merge objects or in function calls or create character arrays from string literals.
For example,
<cfscript> beatles={ "vocals":"John Lennon", "guitar":"George Harrison", "bass":"Paul McCartney", "drums":"Ringo Starr" } beatlesCopy={...beatles,'justanotherkey':'justanothervalue'} writeDump(beatlesCopy) </cfscript>
Array literals
<cfscript> numbers = [1,2,3] list = [...numbers, '4', 'five', 6,...numbers] writeDump(list) </cfscript>
String literals
<cfscript> arr = [..."12345","foo",..."bar",..."123"]; writeDump(arr) </cfscript>
Struct using key-value pair
<cfscript> obj1 ={ foo: 'bar', x: 42 }; obj2 ={ foo: 'baz', y: 13 }; newObj = {...obj1,...obj2}; writeDump(newObj) </cfscript>
You cannot use a spread operator in a Built-In Function. The snippet below will not run as expected.
<cfscript>
myarray1=[1,2,3];
myarray= ArrayNew(1, true,"numeric")
writedump(myarray.append(...myarray1).append("4").append("5"))
for (i in myarray){
writeoutput(i)
}
</cfscript>
Merge Iterable objects
You can use the spread operator to combine a single value from several other values.
For example,
<cfscript> s1=["Mick Jagger"] s2=["Keith Richards","Ron Wood"] s3=["Charlie Watts"] // merge the arrays using Spread stones=[...s1,...s2,...s3] writeDump(stones) </cfscript>
You can also place the spread array inside another array.
For example,
<cfscript> s1=["Mick Jagger"] s2=["Keith Richards","Ron Wood"] s3=["Charlie Watts"] // merge the arrays using Spread stones=["Brian Jones","Bill Wyman",...s1,...s2,...s3] writeDump(stones) </cfscript>
Object literals
<cfscript> foo={ "key1":"val1", "key2":"val2", "key3":"val3" } bar={ "key4":"val4", "key5":"val5" } combo={...foo,...bar} writeDump(combo) </cfscript>
In case of duplicate keys, the duplicate keys are overwritten.
<cfscript> foo={ "key1":"val1", "key2":"val2", "key3":"val3" } bar={ "key3":"val4", "key5":"val5" } combo={...foo,...bar} writeDump(combo) </cfscript>
Functions
Insert arguments into a function by using the Spread operator.
<cfscript> function calcVolume(width, height, depth) { writeOutput(width * height * depth) } calcVolume(12, 30, 14) // the usual way cube = [12, 30, 14] // using Spread operator calcVolume(...cube) </cfscript>
Rest operator
The Rest operator appears the same as Spread (…) but operates just as opposite. Using the Rest operator, you can create an array of an indefinite number of arguments.
For example,
<cfscript> function myRest(...args){ writeDump(args) } myRest(1,2,3,4,5) </cfscript>
All the arguments passed to the myRest function are now available in the args array.
Let’s see another example for Rest.
<cfscript> function myFun(a,b,...otherArgs){ writeOutput(serializeJSON(a)) writeOutput(serializeJSON(b)) writeOutput(serializeJSON(otherArgs)) } myFun("one","two","three","four","five","six","seven","eight") </cfscript>
Output
"one""two"["three","four","five","six","seven","eight"]
You can use the Rest operator as the only parameter or as the last parameter in a function definition. If used as the only parameter, the Rest operator gathers all arguments. If the Rest operator is used at the end of a list, it will gather every argument that is remaining.
For example,
<cfscript> function restTest(one,two,...args){ writeDump(one) writeDump(two) writeDump(args) } restTest(1,2,3,4,5) </cfscript>
This will take the first two arguments individually and then group the rest into an array.